嘿嘿所有我想弄清楚我从 ADDtab_138269500 点击按钮然后去返回 所需的路径)一直到 span 标记,它首先得到了后记。
JSFIDDLE让你看
到目前为止,我所使用的JS代码似乎并不是这样(因为它没有该标签的数据):
$(document).ready(function() {
$('[data-item-id="ADDtab_138269500"]').click(function() {
var blah = $(this).prev().prev().closest("div").find('span');
console.log(blah);
});
});
HTML:
<div class="input-group" style="margin-top: 40px;">
<span data-id="Writter Status" style="color: rgb(255, 46, 49); position: absolute; z-index: 8; text-transform: uppercase; text-shadow: rgba(150, 150, 150, 0.87) 0px 1px 1px; font-family: Arial, Helvetica, sans-serif; font-size: 10px; left: 13px; top: 14px; width: 300px; letter-spacing: 1px;">Writter Status</span>
<span id="lblBG" style="box-shadow: 0px 5px 6px -4px rgba(0,0,0,0.55);border-radius: 4px; border: 1px solid rgba(204, 204, 204, 1); border-image: none; left: 7px; top: 14px; width: 98px; height: 16px; position: absolute; z-index: 3; background-color: rgba(238, 238, 238, 1);"></span>
<div class="input-group-addon" style="width: 33px;">
<img src="data:image/svg+xml;base64,PD94bWw..." id="iconImg_currentUsers" style="width: 20px; height:20px;">
</div>
<div class="col-sm-10">
<span id="inventory" class="tips form-control input-sm" data_tooltip="example 1" style="width: 500px; background-color: rgb(254, 231, 231); height: 37px;" data-cnt="0"><div class="select3-multiple-input-container">
<span class="select3-multiple-selected-item" data-item-id="1" style="padding-right: 5px;">Pending</span>
<span class="select3-multiple-selected-item" data-item-id="2" style="padding-right: 5px;">Approved</span>
<span class="select3-multiple-selected-item" data-item-id="3" style="padding-right: 5px;">Denied</span>
<span class="select3-multiple-selected-item" data-item-id="ADDtab_138269500" style="padding-right: 5px; background-color: rgb(40, 177, 40); z-index: 50;" data-popmodal_id="185">ADD ITEM</span>
<div style="top: 23px; left: 168.344px;" data-popmodal_id="185" data-popmodal="open" class="popModal animated bottomLeft fadeInBottom">
</div>
<input type="text" autocomplete="off" autocorrect="off" autocapitalize="off" class="select3-multiple-input" placeholder="" style="width: 678px; background-color: rgb(255, 255, 255);"><span class="select3-multiple-input select3-width-detector"></span>
<div class="clearfix"></div>
</div>
</span>
</div>
</div>
<div id=""></div>
我希望实现的是从顶部 span标记获取 data-id =&#34; Writer Status&#34; 的值。
答案 0 :(得分:2)
实现这一目标的最简单方法是将DOM从按钮上升到两个元素的最近父容器,然后find()
span
想要读取data-id
来自,像这样:
$('[data-item-id="ADDtab_138269500"]').click(function() {
var id = $(this).closest("div.input-group").find('span[data-id]').data('id');
console.log(id);
});
&#13;
.input-group {
margin-top: 40px;
}
.input-group .status {
color: rgb(255, 46, 49);
position: absolute;
z-index: 8;
text-transform: uppercase;
text-shadow: rgba(150, 150, 150, 0.87) 0px 1px 1px;
font-family: Arial, Helvetica, sans-serif;
font-size: 10px;
left: 13px;
top: 14px;
width: 300px;
letter-spacing: 1px;
}
.input-group .input-group-addon {
width: 33px;
}
.input-group .input-group-addon img {
width: 20px;
height: 20px;
}
.input-group .select3-multiple-selected-item {
padding-right: 5px;
}
.input-group .select3-multiple-selected-item.highlight {
background-color: rgb(40, 177, 40);
z-index: 50;
}
.input-group .popModal {
top: 23px;
left: 168.344px;
}
.input-group .select3-multiple-input {
width: 678px;
background-color: rgb(255, 255, 255);
}
#lblBG {
box-shadow: 0px 5px 6px -4px rgba(0, 0, 0, 0.55);
border-radius: 4px;
border: 1px solid rgba(204, 204, 204, 1);
border-image: none;
left: 7px;
top: 14px;
width: 98px;
height: 16px;
position: absolute;
z-index: 3;
background-color: rgba(238, 238, 238, 1);
}
#inventory {
width: 500px;
background-color: rgb(254, 231, 231);
height: 37px;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="input-group">
<span data-id="Writter Status" class="status">Writter Status</span>
<span id="lblBG"></span>
<div class="input-group-addon">
<img src="data:image/svg+xml;base64,PD94bWw..." id="iconImg_currentUsers">
</div>
<div class="col-sm-10">
<span id="inventory" class="tips form-control input-sm" data_tooltip="example 1" data-cnt="0">
<div class="select3-multiple-input-container">
<span class="select3-multiple-selected-item" data-item-id="1">Pending</span>
<span class="select3-multiple-selected-item" data-item-id="2">Approved</span>
<span class="select3-multiple-selected-item" data-item-id="3">Denied</span>
<span class="select3-multiple-selected-item highlight" data-item-id="ADDtab_138269500" data-popmodal_id="185">ADD ITEM</span>
<div data-popmodal_id="185" data-popmodal="open" class="popModal animated bottomLeft fadeInBottom"></div>
<input type="text" autocomplete="off" autocorrect="off" autocapitalize="off" class="select3-multiple-input" placeholder="" />
<span class="select3-multiple-input select3-width-detector"></span>
<div class="clearfix"></div>
</div>
</span>
</div>
</div>
&#13;
另外,你真的不应该尽可能使用内联样式。将CSS规则解压缩到外部样式表,如上例所示。请注意HTML阅读和理解的清晰程度。
最后,您的HTML目前无效,因为div
元素不能是span
元素的子元素。