2013-03-25 440 views
0

我试图从使用id.remove()属性的函数中删除<img>标签,但它不起作用。你可以找到的东西,我在下面做一个简单的代码:使用.remove()属性删除img标签jquery

<script> 
function verify(img) 
{ 
    if(/*somecondition*/) 
    removetag_setother(); 
    else 
    //do something 
} 
function removetag_setother() 
{ 
    $("#1").remove(); 
    text="<p>hello</p>"; 
    $("body").append(text); 
} 
</script> 
<body> 
<img id="1" onclick="verify(this)" src="image1.png"> 
</body> 

查看控制台日志,我得到这个消息: 的ReferenceError:$没有定义

+1

你在[控制台]任何错误(https://developers.google.com/chrome-developer-tools/docs/console)?例如,我们看到的代码无法编译。 – 2013-03-25 20:35:02

+4

相当确定的ID不能是数字 – Jack 2013-03-25 20:36:39

+0

它工作正常[这里](http://jsfiddle.net/wirey00/5q6CB/) - 是否有更多的代码可能会导致此失效? – 2013-03-25 20:38:44

回答

0

问题是空ifelse声明。将if(/*somecondition*/)替换为if(true)并注释掉else子句。

<script src="http://code.jquery.com/jquery-1.9.1.js"></script> 
<script> 
function verify(img) 
{ 
    if(true) 
     removetag_setother(); 
    //else 
     //do something 
} 
function removetag_setother() 
{ 
    $("#1").remove(); 
    text="<p>hello</p>"; 
    $("body").append(text); 
} 
</script> 
<body> 

+0

我把/ * somecondition * /只是举个例子。我在开始时在removetag_setother()函数内部有一个提示消息,并显示了它的内容,但是,当执行命令$(“#1”)。remove()时,既没有设置文本var也没有执行.append()命令被执行。 – user1013213 2013-03-25 20:49:12

+0

你的代码中的else子句是否为空?如果是因为它将函数的关闭'}'看作你的其他语句,你会得到一个编译错误。 – clav 2013-03-25 20:56:34

+0

其他子句用于调用另一个函数。我确信对removetag_setother()函数的调用已完成,因为我可以看到放入它的警报消息。当执行$(“#1”)。remove()时,我不知道如何捕获错误。你有任何想法赶上错误? – user1013213 2013-03-25 21:05:30