简体   繁体   English

如何使用jQuery切换ListItem的最后两个孩子

[英]How to toggle the last two childs of ListItem using jQuery

I am having the list item which have some childs like.. 我有一些孩子喜欢的清单项目。

  <li id="li1">Your Text 
      <a id="link1" href="#">click to enter</a>
      <input type="text" id="textbox1" value=""/>
      <input type="button" id="button1" value="Edit" />
   </li>

I want to hide and show the textbox and button when i click the link.. 单击链接时,我想隐藏并显示文本框和按钮。

Please Help me.. 请帮我..

you can do it like this 你可以这样

$('#link1').click(function(){
    $('#li1').children().slice(-2).toggle();
});
​

Live Demo 现场演示

Something like this: 像这样:

$(document).ready(function() {
    $("#idOfList li a").click(function(e) {
        e.preventDefault();
        $(this).siblings().toggle();
    });
});

The above assumes that you have something similar in each li element and you want to do the same action for all of them - for that to work you'd obviously put the id from the containing ul or ol element where I've said "#idOfList" . 上面的假设假设每个li元素中都有类似的内容,并且您希望对所有li元素执行相同的操作-为此,您显然应该将包含ul或ol元素的id放在我说过"#idOfList"

Demo: http://jsfiddle.net/nnnnnn/j8NrE/ 演示: http//jsfiddle.net/nnnnnn/j8NrE/

To do it only for the specific link with id="link1" just change the "#idOfList li a" selector to "#link1" . 仅对具有id="link1"的特定链接执行此操作, 只需"#idOfList li a"选择器更改为"#link1"

By using jQuery .siblings() and .slideToggle() methods you can achieve it easily by the following lines of code: 通过使用jQuery .siblings().slideToggle()方法,您可以通过以下几行代码轻松实现此目的:

jQuery: jQuery的:

$(".list-item a").on("click", function(event){           
    $(this).siblings().slideToggle();
    event.preventDefault();    
});​

HTML: HTML:

<ul>
    <li class="list-item">Your Text 
        <a id="link1" href="#">clike to enter</a>
        <input type="text" id="textbox1" value=""/>
        <input type="button" id="button1" value="Edit" />
    </li>

    <li class="list-item">Your Text 
        <a id="link2" href="#">clike to enter</a>
        <input type="text" id="textbox2" value=""/>
        <input type="button" id="button2" value="Edit" />
    </li>
</ul>​

SEE DEMO 查看演示

Add this to your document: 将此添加到您的文档中:

<script type="text/javascript">
    $(function () {
        $('#link1').click(function () {
            $('#textbox1').toggle();
            $('#button1').toggle();
        });
    });
</script>

See it in action here: 在这里查看实际操作:

http://jsfiddle.net/LC7LT/2/ http://jsfiddle.net/LC7LT/2/

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

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