Jquery:conatins显示所有跨度的警报
我有一个跨度Text goes her
就像Text goes her
$('#contentarea').bind('click',function(e){ e.preventDefault(); if($(e.target).is('span')){ if($(e.target).find(":contains(font-weight: bold;)")){{ alert('bold'); } } });
我没有得到实际结果,如果我点击任何跨度文本它会提醒,请帮助我,我需要找出包含font-weight:bold;
你可以使用属性选择器…(如果你只需要选择包含style属性的span作为字体..)
试试这个
$('span[style^="font-"]')
这应该选择以font-
开头的样式 –
要么
$('span[style="font-weight: bold;"]')
小提琴
$(e.target).find(":contains(font-weight: bold;)")
它总是返回一个jQuery对象集合 – 包括0长度集合。 这不是一个javascript列表,你总是在布尔值中得到true
。
var target = $(e.target); if(target.is('span')){ if(target.css('font-weight') == 'bold'){ alert('bold'); } }
首先是抬头, :contains()
选择器用于DOM节点的内容 ,因此您可以将它与以下HTML一起使用:
Hello World // will alert when clicked Goodbye World // will not alert when clicked
以下是jQuery:
$('#contentarea').bind('click',function(e){ e.preventDefault(); if($(e.target).is('span')){ if($(e.target).is(':contains(Hello)')){ alert($(e.target).text()); } } });
你要做的是查看应用的CSS样式或属性 ,所以你想要做更像这样的事情:
HTML:
Hello World Hello World Hello World
jQuery的:
$('#contentarea').bind('click',function(e){ e.preventDefault(); if($(e.target).is('span')){ if($(e.target).css('font-weight')==="bold" || $(e.target).css('font-weight')==="700"){ alert($(e.target).text()); } } });
尝试:
var isBold =$(e.target).css('font-weight'); if($(isBold === ("bold")|| isBold === 700){ alert('bold'); }
这样就可以检查是否存在CSS属性。