首页 > 解决方案 > Toggle multiple with the same id

问题描述

This shows the hidden numbers on click only on the first occurrence. I am not sure how to make it work if there are multiple <span id="number"> within the same text. I wouldn't even mind if they would all show when one of them is clicked. Or they can show independently. Either way.

<script>
$(function(){
$('#number').click(function() {
$(this).find('span').text( $(this).data('last') );
});
});
</script>

<span id="number" data-last="1234">5678<span>****<span class="showphone">Show</span></span></span>

标签: javascriptjquerytoggle

解决方案


You can query DOM node by attribute

<script>
$(function(){
  $('[data-last]').click(function() {
    $(this).find('span').text( $(this).data('last') );
  });
});
</script>

<span id="number" data-last="1234">5678<span>****<span class="showphone">Show</span></span></span>

推荐阅读