Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何根据特定的按钮点击更改代码?_Javascript - Fatal编程技术网

Javascript 如何根据特定的按钮点击更改代码?

Javascript 如何根据特定的按钮点击更改代码?,javascript,Javascript,有两个红色和蓝色的按钮。 如果单击红色,则必须出现一些“示例代码”,然后移动到蓝色,“示例代码”必须隐藏,“新段落”必须出现,反之亦然。您需要添加onClick侦听器。使用JQuery可以很容易地完成这些任务 类似于 $.('#myRedbutton').click(function(){ //Do the red button stuff. }; $.('#myBluebutton').click(function(){ //Do the blue button stuff. };

有两个红色和蓝色的按钮。
如果单击红色,则必须出现一些“示例代码”,然后移动到蓝色,“示例代码”必须隐藏,“新段落”必须出现,反之亦然。

您需要添加onClick侦听器。使用JQuery可以很容易地完成这些任务

类似于

$.('#myRedbutton').click(function(){
  //Do the red button stuff.
};

$.('#myBluebutton').click(function(){
  //Do the blue button stuff.
};
因为你可以像这样编辑按钮的HTML

 <button onclick="redButton()">Red</button>
红色

单击上面的按钮将调用
redButton()

我希望通过使用Javascript来实现这一点。@Sonu好吧,JQuery是Javascript,但我会编辑我的问题。。等一下,好的,谢谢你。但我希望它由基本的javascriptuse onclick属性实现,这是一个糟糕的特性practice@Ethan你的方法没有我的优势。通常使用纯Javascript被认为是不好的做法。函数showtuff(id){var span=document.getElementById(id);if(span.style.display='none'){span.style.display='block';}else{span.innerHTML=hideStuff(id);}函数hideStuff(id){var span=document.getElementById(id);if(span.style.display='none'){span.style.display='block';}其他{span.innerHTML=showtuff(id);}}

HAI

H R U

var red = document.getElementById('RED');
var blue = document.getELementById('BLUE');
var sampleCode = document.getElementById('SAMPLE');

red.addEventListener('click', function () {
    blue.style.display = 'none'; // hide blue, and you can use this method to hide every element you want
    sampleCode.style.display = 'block'; // display sampleCode element
};