有很多行,看起来像:
<div class="info"><ul><li><b><a href="http://www.domain1.com/10166/a-l-f/" title="ALF" target="_blank">ALF</a></b>[some text here]<a href="http://www.domain2.com/50367/DMS/" title=... blah blah blah
我想删除包含domain1.com的每个链接并仅保留其内容(例如ALF)。 我已经尝试过这段代码:
$('a[href*="domain1"]').removeAttr('href');
但它只会删除 href 标签,而不是整个 a 标签。 我想删除整个标签并只保留其内容。 我也使用了这些代码,但没有发生任何事情,只是出现错误!
$("a[href*="domain1"]").replaceWith(function () {
return $(this).text();
});
&
$('a[href*="domain1"]').contents().unwrap();
&
$("a:contains(domain1)").find("a.link").contents().unwrap();
但仍然没有任何反应!
最佳答案
此处的选择器存在语法错误:
$("a[href*="domain1"]").replaceWith(function () {
return $(this).text();
});
字符串中不能有双引号而不进行转义。尝试:"a[href*=\"domain1\"]"
或对字符串使用单引号,'a[href*="domain1"]'
。这应该可行。
关于javascript - 删除 html 换行代码并仅在行包含某些内容时保留内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32382683/