我的代码如下:
<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
的控制台日志中,我获得了正确的班级名称(作者,买家)
有更好的方法吗?
答案 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')
}