简体   繁体   English

您如何使Div像按钮一样做同样的事情?

[英]How do you make a Div do the same thing as a a button?

I am trying to make a navigation bar for a project and I need a button to call the function for a dropdown menu. 我正在尝试为项目创建导航栏,并且需要一个按钮来调用下拉菜单的功能。 If I have a button, I can't format it the same as a div, and it stands out from the other options. 如果有按钮,则无法将其格式化为与div相同的格式,并且它在其他选项中脱颖而出。 Is there a way to make a div be a button? 有没有一种方法可以使div成为按钮? I have tried putting the onclick attribute on it, but then the drop-down menu doesn't appear in the same spot. 我尝试过在其上放置onclick属性,但是下拉菜单没有出现在同一位置。 Thanks in advance. 提前致谢。

(I am very new to coding, so try to explain things in simple terms) (我对编码非常陌生,因此请尝试用简单的术语进行解释)

I decided to do a hover drop-down menu which makes things a lot easier, but thank you for your answers! 我决定做一个悬停下拉菜单,它使事情变得容易得多,但是感谢您的回答!

You say you are very new to coding. 您说您对编码非常陌生。 Have you read about jquery at all? 您是否完全了解jquery? I would definitely suggest it. 我肯定会建议。 It is very possible to make things happen when a <div> tag is clicked with jquery functions. 当用jquery函数单击<div>标记时,很有可能使事情发生。 Here is an example: 这是一个例子:

$("#IdOfDivTag").click(function(){
    alert("The paragraph was clicked.");
});

You will have to add an id or class attribute to the div tag you want an action to happen with (unless you want the same action for all <div> , in which case you can use "div" in the jquery statement). 您将必须向要执行操作的div标签添加id或class属性(除非您希望对所有<div>使用相同的操作,在这种情况下,您可以在jquery语句中使用“ div”)。

To implement jquery you need at add a jquery library to your code... You can start here: http://www.w3schools.com/jquery/jquery_get_started.asp 要实现jquery,您需要在代码中添加jquery库...您可以从此处开始: http : //www.w3schools.com/jquery/jquery_get_started.asp

There are different ways to do this. 有不同的方法可以做到这一点。 One is to use jQuery (a Javascript extension), like Branden Keck showed it to you. 一种是使用jQuery(JavaScript扩展),就像Branden Keck向您展示的那样。 Another way is to use only html and Javascript: There is a html attribute which creates an Javascript event: 另一种方法是仅使用html和Javascript:有一个html属性创建一个Javascript事件:

<div id="myDiv" onclick="clickEvent()">Some text</div>

clickEvent has to have brackets in it because it's a Javascript Function. clickEvent必须带有方括号,因为它是Javascript函数。 Your Javascript could look like something like this then: 您的Javascript可能看起来像这样:

function clickEvent() {
document.getElementById("myDiv").innerHTML = "Hello World";
}

Here a snippet for that: 这是一个片段:

 function myFunction() { document.getElementById("myDiv").innerHTML = "Hello World without jQuery!"; } $("#myDiv2").click(function() { $("#myDiv2").html("Hello World with jQuery!"); }); $("#myDiv3").click(function() { $("#hiddenDiv").slideToggle(750); // 750 is the sliding time in ms }); 
 #myDiv, #myDiv2, #myDiv3, #hiddenDiv { background: #0000ff; color: #ffffff; transition: 0.5s all; -webkit-transition: 0.5 all; font-family: "Arial"; width: auto; height: auto; font-size: 1.3em; margin: 0.4em; padding: 0.2em; border-left: 1px groove #f0f0f0; border-top: 1px groove #f0f0f0; } #myDiv:hover, #myDiv2:hover, #myDiv3:hover { background: #ffffff; color: #000000; padding: 0.22em; box-shadow: 0.22em 0.22em 0.44em #000000; } #myDiv2, #myDiv3 { background: #ff0000; } #hiddenDiv { background: #00aa00; display: none; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> <div id="myDiv" onclick="myFunction()">Click this div! Without jQuery</div> <div id="myDiv2">Click this div! With jQuery</div> <div id="myDiv3">Toggle hidden Text With jQuery<span style="font-size: 0.7em;font-family:Arial;font-style:italic;"> May lag because of snippet.</span></div> <div id="hiddenDiv">You found me!</div> 

你如何在 DIV 中获得一个按钮而不是调用<div>点击事件</div><div id="text_translate"><p>我有一个它自己的 onClick() 事件。 我希望用户能够单击 div 中的任意位置来执行操作(将卡片翻转过来)。 但是,该 DIV 中也有一个按钮。 对于按钮,我不希望它调用 div 的 onClick() (所以不要翻转卡片)。</p><pre> &lt;div onClick={this.handleClick}&gt; &lt;button onClick={this.buttonClickEvent}&gt;menu&lt;/button&gt; &lt;/div&gt;</pre><p> 关于如何执行此操作的任何想法?</p></div> - How do you get a button in DIV not call the <div> click event

暂无
暂无

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

相关问题 你如何制作一个可变大小的div? - How do you make a variable size div? 你如何使一个div“tabbable”? - How do you make a div "tabbable"? 怎么把div标签变成链接 - How do you make a div tag into a link 如何使div和屏幕一样大? - How do you make a div as big as screen? 如何让按钮在 csv 文件中保存表单数据并同时将您重定向到新页面? - How do you make button save form data on a csv file and redirect you to a new page at the same time? 如何使整个Div成为按钮? - How do I make a whole Div a Button? 如何为计时器制作重置按钮? - How do you make a reset button for a timer? 你如何在 DIV 中获得一个按钮而不是调用<div>点击事件</div><div id="text_translate"><p>我有一个它自己的 onClick() 事件。 我希望用户能够单击 div 中的任意位置来执行操作(将卡片翻转过来)。 但是,该 DIV 中也有一个按钮。 对于按钮,我不希望它调用 div 的 onClick() (所以不要翻转卡片)。</p><pre> &lt;div onClick={this.handleClick}&gt; &lt;button onClick={this.buttonClickEvent}&gt;menu&lt;/button&gt; &lt;/div&gt;</pre><p> 关于如何执行此操作的任何想法?</p></div> - How do you get a button in DIV not call the <div> click event 你如何使Ace编辑器成长并以div相同的方式填充表格单元格? - How do you make Ace Editor Grow and fill a table cell in the same fashion a div does? 滚动时如何使 div 跟随? - How do you make a div follow as you scroll?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM