简体   繁体   English

如何检测javascript中元素的存在?

[英]How to detect the existence of an element in javascript?

var newHead = '<div id="head'+text+'"></div>';
var header = "head"+text;
if (document.getElementById(header) == undefined) {
        //alert(document.getElementById("head"+text));
        $("#result-job").append(newHead);
        var result = '<div id="div' + dataSplit[0] + '"><input type="radio" checked="checked" class="choosenJob" name="rdb_job" value="' + dataSplit[0] + '" id="' + dataSplit[0] + '" /><label for="' + dataSplit[0] + '">' + dataSplit[1] + '</label><br /></div>';
        $("div#" + header).append(result);
    } else {
        var result = '<div id="div' + dataSplit[0] + '"><input type="radio" checked="checked" class="choosenJob" name="rdb_job" value="' + dataSplit[0] + '" id="' + dataSplit[0] + '" /><label for="' + dataSplit[0] + '">' + dataSplit[1] + '</label><br /></div>';
        $("div#" + header).append(result);
    }

Can those script run well ? 那些脚本可以正常运行吗? I want add some new element(div) in the result div but my script didnt work 我想在结果div中添加一些新的element(div),但是我的脚本没有用

fiddle simulation: http://jsfiddle.net/86kH4/5/ 小提琴模拟: http//jsfiddle.net/86kH4/5/

Replace 更换

if (document.getElementById(header) == undefined) {

with

if ($("#" + header).length == 0) {

You must add some quotes around your id, and it'll works fine. 您必须在ID周围加上一些引号,这样才能正常工作。 Also, remove the == undefined , let just Javascript check it : 另外,删除== undefined ,只让Javascript检查它:

if (document.getElementById("header")) {
   /* treatment */
} else {
   /* treatment */
}

http://jsfiddle.net/Q98mv/ - Please compare your snippet with this one and you will find where problem was http://jsfiddle.net/Q98mv/-请将您的代码段与此代码段进行比较,然后您会发现问题出在哪里

$('#chk1').click(function(){
    var ada = "asd";  //ada here will be my dynamic parameters                 
    var neww= '<div id="baru'+ada+'"></div>';

    if($('#baru'+ada).length==0){
        $("#asd").append(neww);
    }else{
        $("#baru"+ada).append("Hellow<br>");
    }
})​;

here you should use a jquery function "live" for example: 在这里,您应该使用jquery函数“ live”,例如:

$("div#" + header).live().append(result);

this function is used when the element was added appending the created elements~~ hope it is useful~~~ 当添加元素到创建的元素后添加时使用此函数~~希望它有用~~~

As I mentioned in my comment, your fiddle has a syntax error, that's why it "does not work". 正如我在评论中提到的那样,您的小提琴有语法错误,这就是为什么它“不起作用”的原因。 Once you fixed the error, you get the expected behaviour: http://jsfiddle.net/fkling/86kH4/28/ . 纠正错误后,您将获得预期的行为: http : //jsfiddle.net/fkling/86kH4/28/


Yes , you can use 是的 ,您可以使用

document.getElementById(header) == undefined

to test whether an element with the ID contain in header exists or not. 测试ID包含在header的元素是否存在。

If no such element exists, getElementById will return null and null == undefined is true . 如果不存在这样的元素,则getElementById将返回null并且null == undefinedtrue With that in mind, it would be even more correct if you were comparing against null : 考虑到这一点,如果将nullnull进行比较,那将更加正确:

document.getElementById(header) === null

That said, since you are using jQuery, I would try to be consistent and not mixing it with plain DOM methods. 就是说,由于您使用的是jQuery,因此我会尽量保持一致,不要将其与纯DOM方法混合使用。 Every jQuery object has a length property containing the number of selected elements. 每个jQuery对象都有一个length属性,其中包含选定元素的数量。 So the jQuery equivalent would be: 因此,jQuery等效项为:

$('#' + header).length === 0

replace this if (document.getElementById(header) == undefined) { with if ($('#header') == undefined) { it may work fine. 替换为if (document.getElementById(header) == undefined) {替换为if ($('#header') == undefined) {可能会正常工作。 by the way you missed quotes for header in your code. 顺便说一句,您错过了代码中标头的引号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM