使用.remove()属性jquery删除img标记
我正在尝试删除使用.remove()属性jquery删除img标记,jquery,Jquery,我正在尝试删除 查看控制台日志记录时,我收到以下消息: ReferenceError:$未定义问题在于空的if和else语句。将if(/*somecondition*/)替换为if(true)并注释掉else子句 <script> function verify(img) { if(/*somecondition*/) removetag_setother(); else //do something } function removetag_setothe
查看控制台日志记录时,我收到以下消息:
ReferenceError:$未定义问题在于空的
if
和else
语句。将if(/*somecondition*/)
替换为if(true)
并注释掉else
子句
<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>
功能验证(img)
{
如果(真)
移除Tag_setother();
//否则
//做点什么
}
函数removetag_setother()
{
$(“#1”).remove();
text=“你好””;
$(“正文”)。追加(正文);
}
您在这方面有任何错误吗?例如,我们看到的代码无法编译。相当确定的ID不能是数字。它可以正常工作-是否有更多的代码可能导致此无法工作?@Jack数字ID已在HTML5中标准化/变得有效。旧的浏览器通常也支持这一点。@Fabricio Matté当IE8及以下版本不存在时,我会记住这一点,希望有一天……我将/*somecondition*/放在这里只是为了举个例子。我在removetag_setother()函数的开头有一条警告消息,它显示了它的内容,但是,当执行命令$(“#1”).remove()时,既没有设置文本变量,也没有执行.append()命令。代码中的else子句是否为空?如果是因为它将函数的closing
}
视为else语句,则会出现编译错误。else子句用于调用另一个函数。我确信对removetag_setother()函数的调用已经完成,因为我可以看到放在其中的警报消息。我不知道在执行$(“#1”).remove()时如何捕获错误。你有没有办法捕捉到这个错误?你用的是什么浏览器?您应该在控制台中看到一个错误。调用$(“#1”).remove()
是您的第一个jQuery调用,您确定页面中包含jQuery吗?我的浏览器是Firefox。我得到了控制台错误:ReferenceError:$未定义
<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>