简体   繁体   English

jQuery一键打开所有div

[英]jquery toggle open all div s on one click

Please take a look at below codes, for whatever reason I am unable to open one div only when I click on the edit link, it opens all divs when I click the edit link. 请查看以下代码,无论出于何种原因,我只有在单击编辑链接时才无法打开一个div,而在单击编辑链接时它将打开所有div。

jQuery jQuery的

$(document).ready(function () {
    $("input:button[name='uploadboy']").click(function () {
        $(this).parent().children('.uploadboy').slideToggle(200, 'swing');
    });
});

HTML 的HTML

<div style="overflow:auto;" class="links-box ">
    <p style="float:left; width:250px;" id="links">
        <input type="button" name="uploadboy" id="uploadboy" value="Uploaded" title="Uploaded" style="text-decoration:none;  color: white; text-shadow:none; background: #0692fe; float:left;" class="g-button">
    </p>
</div>
<div class="uploadboy" width: 600px;min-height:50px;background-color: #F2FDD7;border-radius: 10px;border: 1px solid #8EBD43;">
    <p>content</p>
</div>
<div style="overflow:auto;" class="links-box ">
    <p style="float:left; width:250px;" id="links">
        <input type="button" name="uploadboy" id="uploadboy" value="Uploaded" title="Uploaded" style="text-decoration:none;  color: white; text-shadow:none; background: #0692fe; float:left;" class="g-button">
    </p>
</div>
<div class="uploadboy" width: 600px;min-height:50px;background-color: #F2FDD7;border-radius: 10px;border: 1px solid #8EBD43;">
    <p>content</p>
</div>

example in jsFiddle jsFiddle中的示例

As I mentioned in my comment above, IDs must be unique. 正如我在上面的评论中提到的那样,ID必须是唯一的。 That said, try this: 也就是说,请尝试以下操作:

$("input").click(function () {
    $(this).slideToggle(200, 'swing');
});

jsFiddle example jsFiddle示例

Use the below script, .find method only searches for the descendants ( http://api.jquery.com/find/ ). 使用以下脚本, .find方法仅搜索后代( http://api.jquery.com/find/ )。

$(document).ready(function () {
    $("input:button[name='uploadboy']").click(function () {
        $(this).parent().parent().next('.uploadboy').slideToggle(200, 'swing');
    });
});

What I initially see here that's an issue is that you have 2 input buttons with the same id. 我最初在这里看到的一个问题是,您有2个具有相同ID的输入按钮。 While this may not be the overall issue, you still can't have 2 elements with the same id. 尽管这可能不是总的问题,但是您仍然不能拥有2个具有相同ID的元素。 I also am not sure if this is just generic code you cleaned to ask a question, but your selectors seem pretty complicated. 我也不确定这是否只是您要提问的通用代码,但是您的选择器似乎很复杂。 You attach the .click event to both input buttons, then you go to the buttons parent, which is the paragraph, then you go the child object which is the button. 您将.click事件附加到两个输入按钮上,然后转到父级按钮(即段落),然后转到子级对象(即按钮)。 You are essentially going from point one spot, up a level, then back down a level. 从本质上讲,您是从一个点开始,上一个级别,然后下一个级别。 When the click handler is attached to the button, anytime you click a button, you can reference $(this) to refer to the button. 单击处理程序附加到按钮后,只要单击按钮,就可以引用$(this)来引用按钮。

<input type="button" name="uploadboy" id="button1" />
<input type="button" name="uploadboy" id="button2" />

$(document).ready(function(){
    $("input:button[name='uploadboy']").click(function () {
        $(this).SlideToggle(200, 'swing');
    });
});

If you look at the function that is ran when the input button is clicked, it simply refers to the $(this) object. 如果您查看单击输入按钮时运行的函数,则该函数仅引用$(this)对象。 This is a benefit of jquery and $(this) is the specific button that you clicked. 这是jquery的一个优点,而$(this)是您单击的特定按钮。 Even if there are 20 buttons on the page, whatever button is clicked will be this. 即使页面上有20个按钮,单击该按钮也将是这个。 So in the above example, the button clicked will have the slide toggle occur. 因此,在上面的示例中,单击的按钮将发生幻灯片切换。 You could also navigate the dom off of this if you need to move around like before. 如果您需要像以前一样四处走动,也可以浏览dom。

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

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