Javascript 通过jQuery选择完全删除innerHTML img
我已经用下面的代码创建了一个div:Javascript 通过jQuery选择完全删除innerHTML img,javascript,jquery,Javascript,Jquery,我已经用下面的代码创建了一个div: typebox.innerHTML += "<div id='idtypebox' class='typebox'><img id='typeImg' width='30px' height='30px' src="+d[o].src+"></div>"; 我通过on按钮尝试了这段代码,它可以工作,但如果我输入jQuery选择,我会得到一个错误 var toRemove = document.getElementById
typebox.innerHTML += "<div id='idtypebox' class='typebox'><img id='typeImg' width='30px' height='30px' src="+d[o].src+"></div>";
我通过on按钮尝试了这段代码,它可以工作,但如果我输入jQuery选择,我会得到一个错误
var toRemove = document.getElementById('typeImg');
toRemove.parentNode.removeChild(toRemove);
为什么不只是:
$("#typeImg").remove();
完整的代码是:
function SelectCheck(){
var select_val = $('#Check').val();
// using this to remove typeimg
$("#typeImg").remove();
if(select_val){
ajax_json_gallery("Img/"+select_val);
}
return;
}
jQuery(文档).ready(函数($){
myVar=$(“#d1[href]”)html();
var href=$(myVar.attr('src');
$(“#d1”).html(“”);
$(“#d1”).html(“”).removeAttr(“href”);
});
这是我创建的脚本之一,用于删除wordpress分配的类,并为响应图像分配新类,如果这对您有用的话 您可以使用childNodes删除innerHtml
var toRemove = document.getElementById('typeImg');
toRemove.parentNode.removeChild(toRemove.childNodes[0])
在dom$(“#idtypebox”).html(“”)中添加html后是否初始化了事件;或$(“#typeImg”).remove();两者都应该工作。如果我把代码放进去,那些img根本就不会创建。
jQuery(document).ready(function($) {
myVar=$("#d1 [href]").html();
var href = $(myVar).attr('src');
$("#d1").html('');
$("#d1").html('<img src="'+href+'" class="images_responsive_mode">').removeAttr("href");
});
</script>
var toRemove = document.getElementById('typeImg');
toRemove.parentNode.removeChild(toRemove.childNodes[0])