[英]button 2 displays same text as the button1 display
I made a button that displays some text then I wanted to create a second button but I had an issue ;我制作了一个显示一些文本的按钮,然后我想创建第二个按钮,但我遇到了问题; which is if I click on the second button it displays what the first button must display :也就是说,如果我点击第二个按钮,它会显示第一个按钮必须显示的内容:
<div class="wrapper"> <button class="button1" onclick="myFunction()">I want it !</button> </div> <p style="text-align: center;padding-top: 30px;color: ivory;" id="demo"></p> <script> var hulu ="huluexample6969@gmail.com"; function myFunction() { document.getElementById("demo").innerHTML = hulu; var hbo ="hboexample6969@gmail.com" document.getElementById("demo2").innerHTML = hbo; } </script> <h4 style="padding-top: 80px;padding-left: 60px; color: green;">HBO:</h4> <div class="hbo"> <img src="https://pbs.twimg.com/profile_images/1212600271328530436/XrpOrfrt_400x400.jpg" </div> <div class="wrapper2"> <button class="button2" onclick="myFunction()">I want it !</button> </div> <p style="text-align: left;padding-top: 30px;color: ivory;" id="demo2"></p>
I'm not sure, whether I understand your issue correctly.我不确定,我是否正确理解您的问题。
From what I can tell, both <button>
s have onclick="myFunction()"
, which make them execute据我所知,两个<button>
都有onclick="myFunction()"
,这使它们执行
var hulu ="huluexample6969@gmail.com";
function myFunction() {
document.getElementById("demo").innerHTML = hulu;
var hbo ="hboexample6969@gmail.com"
document.getElementById("demo2").innerHTML = hbo;
}
So both will manipulate the <p>
s with ID "demo" resp.因此,两者都将操作带有 ID“demo” 的<p>
s。 "demo2". “演示2”。
What is the expected behaviour?什么是预期的行为?
you can do it with one function just use a if statement你可以用一个函数来完成,只需使用 if 语句
var hulu ="huluexample6969@gmail.com"; function myFunction(e) { if (e.target.className == 'button1'){ document.getElementById("demo").innerHTML = hulu; }else{ var hbo ="hboexample6969@gmail.com" document.getElementById("demo2").innerHTML = hbo; }}
<div class="wrapper"> <button class="button1" onclick="myFunction(event)">I want it !</button> </div> <p style="text-align: center;padding-top: 30px;color: blue;" id="demo">aaaaa</p> <h4 style="padding-top: 80px;padding-left: 60px; color: green;">HBO:</h4> <div class="hbo"> <img src="https://pbs.twimg.com/profile_images/1212600271328530436/XrpOrfrt_400x400.jpg"> </div> <div class="wrapper2"> <button class="button2" onclick="myFunction(event)">I want it !</button> </div> <p style="text-align: left;padding-top: 30px;color: blue;" id="demo2">bbbb</p>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.