开发者

Hiding elements with a specified id

开发者 https://www.devze.com 2023-03-17 19:50 出处:网络
I have two different divs var promptContent = $(\'<div id=\"errorr\">\').addClass(\"formErrorContent\").html(promptText).appendTo(prompt);

I have two different divs

var promptContent = $('<div id="errorr">').addClass("formErrorContent").html(promptText).appendTo(prompt);
var arrow = $('<div id="errorrarrow">').addClass("formErrorArrow");

I want to use their id in javascript like this

  function windowclose()
{
    document.getElementById('add_project').style.display="none";
    document.getElementById('blanket').style.display="none";
    document.getElementById('errorr').style.display="none";
    document.getElementById('errorrarrow').style.display="none";

//     $("#blanket").fadeIn("none");
//        $("#add_project").fadeIn("none");
}

But here it hi开发者_开发问答des only the 1st div. I want to hide all the divs with the same id. How can I do this?


Choose between to use name or id:

getElementsByName('errorr') searches in the DOM an element named errorr

getElementById('errorr') searches in the DOM an element with the id equals to errorr;

Try to change your code:

document.getElementsByName('errorr').style.display="none";
document.getElementsByName('errorrarrow').style.display="none";

to

document.getElementById('errorr').style.display="none";
document.getElementsById('errorrarrow').style.display="none";

also note that a DOM must have all unique IDs (as rule)

UPDATE:

Well, if you need to hide a set of divs I usually add at all of them a class like .element-to-hide:

<div id="asd" class="element-to-hide">...
<div id="lol" class="element-to-hide">...
<div id="foo" class="element-to-hide">...

Ant after just a touch of jQuery:

$('.element-to-hide').each(function(){
  $(this).hide();
});

Hope this help


Even though using the same ID on many elements is semantically invalid, you could do it in a single jQuery line.

$('#errorr, #errorrarrow').hide();
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号