Javascript 需要帮助确定在jQuery中删除元素及其内容不起作用的原因吗
有人能告诉我这个jQuery代码有什么问题吗 $'.deleter'。单击函数{ var toDelete=$this.attr'stringStorage'; $'.url.text$'.url.text.replacetoDelete; $this.remove; }; 德雷特先生{ 显示:内联块; 右边距:10px; 背景色:红色; 颜色:白色; 光标:指针; } .deleter:hover{背景色:橙色;} br{线高:22px;} xwww.firsturl.com xwww.anotherl.com xwww.athirdurl.com 选中此项: 修改HTML,如下所示:Javascript 需要帮助确定在jQuery中删除元素及其内容不起作用的原因吗,javascript,jquery,Javascript,Jquery,有人能告诉我这个jQuery代码有什么问题吗 $'.deleter'。单击函数{ var toDelete=$this.attr'stringStorage'; $'.url.text$'.url.text.replacetoDelete; $this.remove; }; 德雷特先生{ 显示:内联块; 右边距:10px; 背景色:红色; 颜色:白色; 光标:指针; } .deleter:hover{背景色:橙色;} br{线高:22px;} xwww.firsturl.com xwww.an
<div class="urls">
<div><span class="deleter" stringStorage="www.firsturl.com">x</span>www.firsturl.com</div>
<div> <span class="deleter" stringStorage="www.anothereurl.com">x</span>www.anothereurl.com</div>
<div><span class="deleter" stringStorage="www.athirdurl.com">x</span>www.athirdurl.com</div>sss
</div>
可以使用.nextSibling、.textContent将单击的.deleter元素旁边的文本节点设置为空字符串
您还可以在html中使用data-*属性替换自定义属性
试试这个
HTML
哎呀,你让它看起来很简单。非常感谢。我会从中学到很多,因为我以后会多次引用它。谢谢伊什,谢谢你的回答,但我应该提到我不能弄乱html结构。谢谢你的回答库马尔,我忘了强调这只是我需要帮助的jquery,无法调整html。。
$('.deleter').click(function() {
var toDelete = $(this).attr('stringStorage');
$(this).parent().remove();
});
$('.deleter').click(function(e) {
var toDelete = $(this).data('stringStorage');
this.nextSibling.textContent = "";
$(this).remove();
});
<div class="urls">
<span class="deleter" data-string-storage="www.firsturl.com">x</span>www.firsturl.com<br>
<span class="deleter" data-string-storage="www.anothereurl.com">x</span>www.anothereurl.com<br>
<span class="deleter" data-string-storage="www.athirdurl.com">x</span>www.athirdurl.com<br>
</div>
$(".urls").contents().filter(function() {
return this.nodeType === 3 && this.nodeValue.trim() === toDelete
}).add(this).remove();
<div class="urls">
<div class="delete">
<span class="deleter" stringStorage="www.firsturl.com">x</span>www.firsturl.com<br>
</div>
<div class="delete">
<span class="deleter" stringStorage="www.anothereurl.com">x</span>www.anothereurl.com<br>
</div>
<div class="delete">
<span class="deleter" stringStorage="www.athirdurl.com">x</span>www.athirdurl.com<br>
</div>
</div>
$('.delete').click(function() {
var toDelete = $(this).attr('stringStorage');
$(this).remove();
});