我有一些使用PHP创建的div.div中的锚点总是有一个HREF,即使它是空白的.基本上,我试图检测HREF是否为空白.如果它有内容,则不执行任何操作,如果它为空白,则删除文本,删除锚点,然后将文本放回原位.
这是div:
<div class="title">
<a class="article" href="">Lorem Ipsum</a>
</div>
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
jQuery(document).ready(function($) { //required for $ to work in Wordpress
$(".article").each(function(){
if ($(this).attr('href') !== undefined) {
return;
} else {
var linkTitle = $(this).html();
$(this).parent().empty().html(linkTitle);
}
});
//-->
});
Run Code Online (Sandbox Code Playgroud)
Nic*_*ver 16
您可以检查空href属性并使用以下方式"解包"这些链接.replaceWith():
$(".article[href='']").replaceWith(function() { return this.innerHTML; });
Run Code Online (Sandbox Code Playgroud)
mea*_*gar 15
您可以简单地将属性测试为布尔值,而不是对其进行测试undefined:
if ($(this).attr('href')) {
// href is not blank
} else {
// href is blank
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
31115 次 |
| 最近记录: |