如何在div onclick下添加按钮

加布里埃尔·奥古斯托(Gabriel Augusto)

我有一堆并排的div卡,使用以下显示:inline-block,而我想做的是单击卡时,它在div下方添加了一个按钮

我正在使用jquery的after()函数来做到这一点,但是它在div右边添加了按钮

拜托,我该怎么做?

PS:我尝试使用append函数,但是当再次单击div时,我还有另一个函数可以删除按钮,因此,当我单击按钮(现在是div的一部分)时,就像单击了div一样,并且该按钮被删除,因此它不起作用...

谢谢!

编辑...

非常感谢您的回答...

很抱歉没有显示代码,这里是:

    <div class = "classOfDiv">
        here goes some product characteristics
    </div>


$('.classOfDiv').click(
    function() {
        //gets the border because I need it to see if I'm clicking for the first time or second time.
        var border = $(this).css("border");

        //checks the border
        if (border == "0px none rgb(51, 51, 51)") { 
            //change the border style to say that the div has been clicked
            $(this).css("border", "3px solid blue");
            //add the button
            $(this).after("<button name = 'btnDetalhes' class = 'btn btn-primary'>Detalhes</button>");  
        } else {
            //remove the border (now the div is diselected)
            $(this).css("border", "0px none rgb(51, 51, 51)");  
            //remove the button
            $(this).next("[name='btnDetalhes']").remove();
        }
    }
);
ni缩

由于您还没有实际答案,因此这是一个关于如何执行我在显示/隐藏中建议的操作的粗略概念。

您将需要指定“隐藏”按钮div的内容。如果它应该再次单击该卡,则只需更改show()toggle

.card {
  display: inline-block;
  position: relative;
  width: 100px;
  height: 100px;
  border: 1px solid black;
  margin: 5px;
  text-align: center;
  cursor: pointer;
  }

.card-button {
  display: none;
  position: absolute;
  bottom: -22px;
  left: -1px;
  width: 100%;
  height: 20px;
  border: 1px solid red;
  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
  <li class="card" onclick="$('.card-button').show()">
    <p>Card Content</p>
    <div class="card-button" onclick="alert('button clicked!')">
      <span>BUTTON</span>
    </div>
  </li>
</ul>

根据对话,另一种解决方案是event.stopPropogation()在事件处理程序中使用,以防止对按钮的单击触发其他父级的单击事件。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何从创建选项卡内容添加按钮 onclick

来自分类Dev

添加按钮 - 如何?

来自分类Dev

如何添加“添加按钮”?

来自分类Dev

如何在RecyclerView的末尾添加按钮?

来自分类Dev

如何在InfoWindow中添加按钮?

来自分类Dev

如何在标题中添加按钮

来自分类Dev

如何在MKPointAnnotation中添加按钮?

来自分类Dev

如何在列表上方添加按钮?

来自分类Dev

如何在SwiftUI中添加按钮

来自分类Dev

如何在标题中添加按钮

来自分类Dev

如何在边框中添加按钮?

来自分类Dev

如何在 GMSMapView 前面添加按钮

来自分类Dev

如何在文本下添加按鈕

来自分类Dev

如何动态添加按钮

来自分类Dev

如何在部分关闭按钮上添加按钮?

来自分类Dev

如何在onclick按钮上更改div下的样式

来自分类Dev

如何动态添加按钮和总计?

来自分类Dev

Java:如何向框架添加按钮?

来自分类Dev

如何添加按钮XML的对话吗?

来自分类Dev

Bootbox-如何动态添加按钮?

来自分类Dev

如何向python添加按钮

来自分类Dev

如何向导航栏添加按钮?

来自分类Dev

Android studio,如何给标签添加按钮

来自分类Dev

如何在Qt的主窗口中添加按钮?

来自分类Dev

如何在活动地图中添加按钮?

来自分类Dev

如何在jqgrid中添加按钮/图像的新列

来自分类Dev

如何在视频代码中添加按钮叠加层

来自分类Dev

如何在JavaFX表格视图中添加按钮

来自分类Dev

如何在tabcontrol标签页中添加按钮