我在div里面有一个div.这个内部div有一个数据属性.我试图应用这个问题的答案找到内部div,但我没有运气.我究竟做错了什么?
var content = $('#mapElements').find('.mapMarker [data-depotid="b04b4184"]').data('depotguid');
$('#result').text(content);Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id='mapElements'>
<div class="mapMarker" data-depotid="b04b4184">This is the text from mapMarker, and if the depotID appears below this text the code works!</div>
</div>
<div id='result'></div>Run Code Online (Sandbox Code Playgroud)
用于定位.mapMarker元素的错误选择器.使用:
var content = $('#mapElements').find('.mapMarker[data-depotid="b04b4184"]').data('depotguid');
//^space not required here
Run Code Online (Sandbox Code Playgroud)
您还可以将子元素的选择器缩小到:
var content = $('#mapElements').find('.mapMarker').data('depotguid');
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
59 次 |
| 最近记录: |