Jquery 基于数据atribute和href添加类
我有以下html代码:Jquery 基于数据atribute和href添加类,jquery,Jquery,我有以下html代码: <a class="infoBox" href="#mnModal">Open modal</a> <div class="modal" data-modal="mnModal"> <button>X</button> <p>Hello</p> </div> 比特它不工作:-( 为什么不呢?是语法错误还是概念错误? html标记会重复几次,因此它必须基于href标记
<a class="infoBox" href="#mnModal">Open modal</a>
<div class="modal" data-modal="mnModal">
<button>X</button>
<p>Hello</p>
</div>
比特它不工作:-(
为什么不呢?是语法错误还是概念错误?
html标记会重复几次,因此它必须基于href标记之类的内容。我不能只使用类作为选择器。我认为你做得不对。 您最好使用jQuery的
data()
方法来检索数据模式
属性
$('.infoBox').click(function(){
var addressValue = $(this).attr("href").replace('#', '') ;
if($('.modal').data('modal') == addressValue){
$('.modal').addClass('open');
}
});
或者,如果存在多个具有相同类名“modal”的modal,则最好使用:
$('.infoBox').click(function(){
var addressValue = $(this).attr("href").replace('#', '') ;
$('.modal').each(function(){
if($(this).data('modal') == addressValue){
$(this).addClass('open');
}
});
});
我认为你做得不对。 您最好使用jQuery的
data()
方法来检索数据模式
属性
$('.infoBox').click(function(){
var addressValue = $(this).attr("href").replace('#', '') ;
if($('.modal').data('modal') == addressValue){
$('.modal').addClass('open');
}
});
或者,如果存在多个具有相同类名“modal”的modal,则最好使用:
$('.infoBox').click(function(){
var addressValue = $(this).attr("href").replace('#', '') ;
$('.modal').each(function(){
if($(this).data('modal') == addressValue){
$(this).addClass('open');
}
});
});
find()方法返回所选元素的子元素
后代是孩子、孙子、曾孙等等
它不包括元素本身
链接:
用这个代替
$(".modal[data-modal='"+addressValue+"']").addClass('open');
find()方法返回所选元素的子元素
后代是孩子、孙子、曾孙等等
它不包括元素本身
链接:
用这个代替
$(".modal[data-modal='"+addressValue+"']").addClass('open');
不能在以引号为界的常规字符串中使用
${expression}
插值。必须是一个使用倒勾。不能在以引号为界的常规字符串中使用${expression}
插值。必须是一个使用倒勾。我完全误解了.find()
然后。谢谢。我完全误解了.find()
然后。