[英]Stripping out a link in jQuery
我有一点像这样的HTML:
<a href="#somthing" id="a1"><img src="something" /></a>
<a href="#somthing" id="a2"><img src="something" /></a>
我需要剥离链接,所以我只剩下几个图像标签。 使用jQuery执行此操作的最有效方法是什么?
$("a > img").parent() // match all <a><img></a>, select <a> parents
.each( function() // for each link
{
$(this).replaceWith( // replace the <a>
$(this).children().remove() ); // with its detached children.
});
这应该这样做:
$('a[id^=a]').each(function() { $(this).replaceWith($(this).html()); });
在普通的JavaScript中,它将是这样的:
<script type="text/javascript">
window.onload = function(){
var l = document.getElementsByTagName("a");
for(i=0, im=l.length; im>i; i++){
if(l[i].firstChild.tagName == "img"){
l[i].parentNode.replaceChild(l[i].firstChild,l[i]);
}
}
}
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.