简体   繁体   English

单击按钮后显示div jQuery

[英]Show div after button click jquery

Hi I try to show a div element in jQuery mobile when the user touch the button. 嗨,我尝试在用户触摸按钮时在jQuery mobile中显示div元素。 I already created own classes for the button and for the div element, but nothing happens. 我已经为按钮和div元素创建了自己的类,但是什么也没有发生。 What classes should I take? 我应该上什么课?

JS: JS:

    $(document).ready(function(){
 $(".commentbtn").on("click", function(e) {
    e.preventDefault(); // in some browsers a button submits if no type=
    $(this).siblings("div.comment").toggle();
});
});

CSS: CSS:

.comment {
    display:none;
}

HTML: HTML:

<?php foreach ($result as $key => $row): ?>
    <div class="ui-btn-text">
        <button class="commentbtn" data-rel="button">comment</button>
        <div id="createcomment" class="comment" data-theme="a">
            <form data-ajax="false" name="login-form" class="login-form" action="./comments.php" method="post" style="padding:20px 40px;">
                <div class="content">
                    <textarea rows="1" name="text" id="text" class="foo"></textarea>
                </div>
            </form>
        </div>
    </div>
<?php endforeach; ?>

Your button has class commentbtn, so you should use that instead of btn-info. 您的按钮具有class commentbtn类,因此您应该使用该类而不是btn-info。 Also, you should be looking for the sibling div, not the closest. 另外,您应该在寻找同级div,而不是最接近的div

$(".commentbtn").on("click", function(e) {
   e.preventDefault(); // in some browsers a button submits if no type=
   $(this).siblings("div.comment").show();
});

JSFiddle demo JSFiddle演示

You haven't got any element with the class .btn-info so you wouldn't be able to call the event from: 您的类.btn-info没有任何element ,因此您将无法从以下方式调用事件:

  $(".btn-info").on("click", function(e) {
    e.preventDefault(); // in some browsers a button submits if no type=
    $(this).closest(".comment").children(".comment").show();
  });

You have an element with the class .commentbtn which you would then do the same as you did with the .btn-info 您有一个.commentbtn类的元素,然后您将对该元素进行与.btn-info

  $(".commentbtn").on("click", function(e) {
    e.preventDefault(); // in some browsers a button submits if no type=
    // Console log the element
    console.log($(this).closest(".comment").children(".comment"));
    // Console log not showing the right element. So you need to get the 
    // sibling of the clicked button
    // Instead of doing - $(this).closest(".comment").children(".comment").show();
    // You can do the following
    $(this).siblings("div.comment").show();
  });

.closest() - looks at the element itself and its parents for a match. .closest() -查看元素本身及其父元素是否匹配。

.siblings - Get the siblings of each element in the set of matched elements, optionally filtered by a selector. .siblings获取匹配的元素集中每个元素的同级,可以选择由选择器过滤。

Example With .show(); .show();示例

Here an example with .toggle(); 这是一个带有.toggle();的例子.toggle(); If you wanted to show/hide the comment with same button. 如果您想显示/隐藏具有相同按钮的评论。 (Just little extra for you to look at) (只需要一点额外的钱就可以看到)

Example With .toggle(); .toggle();示例

UPDATE: 更新:

Example with .comment shown on load 负载显示.comment示例

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

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