从 html() 中删除一个元素?
如何删除 html() 中的某些元素?
例如,我有这个html链接,
<ul>
<li><a href="#" class="string-comment">Comment</a></li>
<li><a href="#" class="string-delete-needle">Delete</a></li>
<li><a href="#" class="string-publish-needle">Publish</a></li>
</ul>
当你点击string-publish-needle
类时,我会将html存储在一个变量中,
var html_parents = object_parents.html();
<li><a href="#" class="string-comment">Comment</a></li>
<li><a href="#" class="string-delete-needle">Delete</a></li>
<li><a href="#" class="string-publish-needle">Publish</a></li>
我想删除这个
,string-publish-needle
的 >parent
<li><a href="#" class="string-publish-needle">Publish</a></li>
所以我只能在下面有一个新的 html 内容,
<li><a href="#" class="string-comment">Comment</a></li>
<li><a href="#" class="string-delete-needle">Delete</a></li>
我的代码当然不起作用!
$('.string-publish-needle').click(function(e){
var object = $(this);
var object_path = object.attr('href');
var object_parent = object.parent('li');
var object_parents = object.parents('ul');
var html_parents = object_parents.html();
var html_remain = object_parents.html().remove(object_parent);
alert(html_remain);
});
How can I remove certain element inside html()?
For instance, I have this html links,
<ul>
<li><a href="#" class="string-comment">Comment</a></li>
<li><a href="#" class="string-delete-needle">Delete</a></li>
<li><a href="#" class="string-publish-needle">Publish</a></li>
</ul>
When you click on the class of string-publish-needle
, I will store the html in a variable,
var html_parents = object_parents.html();
<li><a href="#" class="string-comment">Comment</a></li>
<li><a href="#" class="string-delete-needle">Delete</a></li>
<li><a href="#" class="string-publish-needle">Publish</a></li>
and I want to remove this one which is the parent
of string-publish-needle
,
<li><a href="#" class="string-publish-needle">Publish</a></li>
so I can have a new html content below only,
<li><a href="#" class="string-comment">Comment</a></li>
<li><a href="#" class="string-delete-needle">Delete</a></li>
My code but does not work of course!
$('.string-publish-needle').click(function(e){
var object = $(this);
var object_path = object.attr('href');
var object_parent = object.parent('li');
var object_parents = object.parents('ul');
var html_parents = object_parents.html();
var html_remain = object_parents.html().remove(object_parent);
alert(html_remain);
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这有帮助吗?
Does this help ?
不知道我理解得好不好...
但
你可以做
分离函数 jQuery
I don't know if I understood well...
but
you can do
detach function jQuery