how i can select this element with Jquery?

左心房为你撑大大i 提交于 2020-01-06 04:33:22

问题


in the code bellow how can i select the span element with id="read-more" if iam in the anchor element with id="preface-more-btn" using $(this) (to be applied for more than one button) in jquery?

<pre>
   <div id="content">
       <p>
           <span style="font-size: 16px;"></span>
       </p>
       <span id="read-more" style="display: none;"></span>
       <div class="more active" id="preface-more">
           <a class="btn-more" id="preface-more-btn" href="#"></a>
       </div>
   </div>
</pre>

thank you for helping..


回答1:


var selector = $('#read-more');

It is globally unique since it is has an ID.

EDIT: Try

$(this).parent().prev();



回答2:


If you're contextually within the element with id="reface-more-btn", you can select it intuitively like this:

$(this).parent().parent().find('#read-more');

If you're looking for readability, try this:

$(this).closest('#read-more');

But since the id is unique to only one element, why aren't you just using $('#read-more');?

You might be confusing class and id, as only one element can have a certain id, while many elements can share a class. Read this question for a longer explanation, as it's essential to know the differences: div class vs id.




回答3:


This will always work:

$("#read-more");



回答4:


Given that the structure will always look like it does:

var theSpan = $(this).parent().parent().find('#read-more');



回答5:


Well, selecting an element by id is quite easy.

$("#read-more")

Will do it regardless of where you are because the id's are global.




回答6:


$('#read-more') will do the trick


来源:https://stackoverflow.com/questions/5561711/how-i-can-select-this-element-with-jquery

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!