如何在if语句中获取属性的值?

时间:2017-08-29 09:17:02

标签: jquery ajax

我的代码如下:

<div class="follow-me">
    <div class="author-follow"> 
    <a data-followed-by="author buyer" class="meta-badge">Follow</a>
    </div>
</div>

var $container          = $(this).closest('.follow-me');    
var followed_status     = $container.find('.author-follow a.meta-badge').attr('data-followed-by');

我尝试使用

if ($( "a[data-followed-by*='author']" ) ) {
    //do something ...
}

并尝试了

if (typeof(followed_status) == 'author' ) {
    //do something ...
}

if (typeof(followed_status) == 'buyer' ) {
    //do something else ...
}

但这些解决方案都没有正常运作。

followed_status的控制台日志中,我获得了正确的班级名称(作者,买家)

有更好的方法吗?

2 个答案:

答案 0 :(得分:0)

如果 attr a包含&#34;买家&#34; {{1 }}

data-followed-by

答案 1 :(得分:0)

尝试这个

if ( $(".meta-badge").attr('data-followed-by') === 'author' ) {
   //do something ...
   console.log('author');
} else if ( $(".meta-badge").attr('data-followed-by') === 'buyer' ) {
   //do something ...
   console.log('buyer')
} else if ( $(".meta-badge").attr('data-followed-by') === 'author buyer' ) {
   //do something ...
   console.log('author buyer')
} else{
   console.log('None')
}