如何检查jquery中的空attr()? [英] How do I check for empty attr() in jquery?

查看:97
本文介绍了如何检查jquery中的空attr()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些使用PHP创建的div. div中的锚点始终为HREF,即使它为空.基本上,我试图检测HREF是否为空.如果有内容,则不执行任何操作;如果为空白,则删除文本,删除锚点,然后将文本放回原处.

I have a few divs that are created using PHP. The anchor within the div always has a HREF, even if it is blank. Basically, I am trying to detect if the HREF is blank. If it has content, do nothing, if it's blank, strip the text out, delete the anchor, them put the text back in.

这是div:

<div class="title"> 
    <a class="article" href="">Lorem Ipsum</a> 
</div> 

这是我的代码:

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);
        }                               
    });    
//-->
});

推荐答案

您可以检查一个空的href属性,然后使用

You can check for an empty href attribute and "unwrap" those links using .replaceWith() like this:

$(".article[href='']").replaceWith(function() { return this.innerHTML; });

您可以在这里尝试.

这篇关于如何检查jquery中的空attr()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆