Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
276 views
in Technique[技术] by (71.8m points)

javascript - 如何找到jQuery中是否存在具有特定id的div?(How to find if div with specific id exists in jQuery?)

I've got a function that appends a <div> to an element on click.(我有一个函数在点击时将<div>附加到元素上。)

The function gets the text of the clicked element and assigns it to a variable called name .(该函数获取被点击元素的文本,并将其分配给名为name的变量。) That variable is then used as the <div> id of the appended element.(然后将该变量用作附加元素的<div> id 。) I need to see if a <div> id with name already exists before I append the element but I don't know how to find this.(我需要查看在添加元素之前是否已存在带有name<div> id ,但我不知道如何找到它。) Here is my code:(这是我的代码:) $("li.friend").live('click', function() { name = $(this).text(); // if-statement checking for existence of <div> should go here // If <div> does not exist, then append element $("div#chatbar").append("<div class='labels'><div id='" + name + "' style='display:none;'></div>" + name + "</div>"); // Else alert('this record already exists'); }); This seems pretty straightforward but I'm getting the error “ Unexpected end of file while searching for class name” .(这看起来非常简单,但我收到错误“ 搜索类名时文件意外结束” 。) I have no clue what that means.(我不知道这意味着什么。) if (document.getElementById(name)) { $("div#" + name).css({bottom: '30px'}); } else { $("div#page-content div#chatbar").append("<div class='labels'>" + name + "</div><div id='" + name + "'></div>"); } What's more is that I want to be able to delete this element if I close it out which should then remove the div id [name] from the document but .remove() does not do this.(更重要的是,我希望能够删除此元素,如果我将其关闭,然后从文档中删除div id [name] ,但.remove()不会这样做。) Here is the code for that:(这是代码:) $(".mini-close").live('click', function(){ $(this).parent().remove(); }); I added .mini-close to the append function as a child of .labels so there was a way to close out of the appended <div> if needed.(我加.mini-close的附加功能的一个子.labels所以有一个办法,关闭追加出来<div>如果需要的话。) After clicking .mini-close and attempting to click the same name again from li.friends it still finds the div id [name] and returns the first part of my if statement.(单击.mini-close并尝试再次从li.friends单击相同的名称后,它仍会找到div id [name]并返回我的if语句的第一部分。)   ask by sadmicrowave translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use .length after the selector to see if it matched any elements, like this:(您可以在选择器后面使用.length来查看它是否与任何元素匹配,如下所示:)

if($("#" + name).length == 0) { //it doesn't exist } The full version:(完整版:) $("li.friend").live('click', function(){ name = $(this).text(); if($("#" + name).length == 0) { $("div#chatbar").append("<div class='labels'><div id='" + name + "' style='display:none;'></div>" + name + "</div>"); } else { alert('this record already exists'); } }); Or, the non-jQuery version for this part (since it's an ID):(或者,这部分的非jQuery版本(因为它是一个ID):) $("li.friend").live('click', function(){ name = $(this).text(); if(document.getElementById(name) == null) { $("div#chatbar").append("<div class='labels'><div id='" + name + "' style='display:none;'></div>" + name + "</div>"); } else { alert('this record already exists'); } });

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...