简体   繁体   English

HTML:jQuery:DOM操作

[英]HTML: jQuery: DOM manipulation

I'm lazy. 我很懒。 There! 那里! That's out of the way. 那是不对的。

I have a list of items of indeterminate length which I would like to render in HTML. 我有一个长度不确定的项目列表,我想用HTML呈现。 I want to show a part of each item and then have a "show more" link in each item which will show more of the item. 我想显示每个项目的一部分,然后在每个项目中显示一个“显示更多”链接,以显示更多的项目。 Since the "show more" link implements the same functionality, I bound the same click handler function to the "show more link" which then shows the HTML span which contains the hidden text. 由于“显示更多”链接实现了相同的功能,因此我将相同的单击处理程序功能绑定到“显示更多链接”,然后显示包含隐藏文本的HTML跨度。 My code looks like this: 我的代码如下所示:

<html>
<head>
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.6.4.min.js"></script> 
</head>
<body>
<p>List of stuff</p>
<ul>
    <li> One <a href="#" class="showmore_link">show more</a>  <span class="showmore" style="display:none"> More about One </span></li>
    <li> Tow <a href="#" class="showmore_link">show more</a>  <span class="showmore" style="display:none"> More about Two </span></li>
    <li> Three <a href="#" class="showmore_link">show more</a>  <span class="showmore" style="display:none"> More about Three </span></li>
</ul>
<script type="text/javascript">

     $(document).ready(function() {
        $(".showmore_link").click(show_more_toggle);
     });

     show_more_toggle=function(){
        $(".showmore").slideToggle();
     }

</script>
</body>
</html>

When I click the "show more" link, it shows all three spans of class "showmore". 当我单击“显示更多”链接时,它将显示类“显示更多”的所有三个范围。 How can I adjust the "show_more_toggle" click handler so that it only opens up the span in the same list item? 如何调整“ show_more_toggle”点击处理程序,使其仅打开同一列表项中的跨度?

CONSTRAINT: I cannot use "id" attributes on the spans or the list elements for other reasons. 约束:由于其他原因,我不能在范围或列表元素上使用“ id”属性。

$('a.showmore_link').click(function() {
    $(this).next().show();
});

Try - 尝试-

 show_more_toggle=function(){
    $(this).siblings(".showmore").slideToggle();
 }

Demo - http://jsfiddle.net/ipr101/9GB9u/ 演示-http://jsfiddle.net/ipr101/9GB9u/

another solution: 另一个解决方案:

$(document).ready(function() {
    $(".showmore_link").click(function() {
        $(this).parent().children(".showmore").slideToggle();
    });
});

http://jsfiddle.net/A7fM5/ http://jsfiddle.net/A7fM5/

使用next() -它将使您获得span元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM