简体   繁体   English

在 onClick 属性中声明变量,并在 onClick 中使用多个命令

[英]Stating variables in the onClick attribute, and using multiple commands in onClick

Alrighty then.那好吧。 Before I get started, I did search for this, I realize there are other threads on this topic, but none of them answered my question well.在开始之前,我确实搜索了这个,我意识到这个主题还有其他主题,但没有一个很好地回答了我的问题。

So what I am trying to do is when a tag is clicked, it creates a variable inside of the onClick attribute that can be accessed from a function to be used with a case switch.所以我想要做的是当一个标签被点击时,它会在 onClick 属性内创建一个变量,可以从一个与 case switch 一起使用的函数访问该变量。

1) Can variables be created in the onClick attribute? 1) 可以在 onClick 属性中创建变量吗?
2) Can multiple commands be executed in the onClick attribute? 2) onClick 属性中可以执行多个命令吗? Like : onClick="alert('Boo!'); afunction();"像: onClick="alert('Boo!'); afunction();" ? ?

I apologize for the lack of code, but I am using an apple iPad, and they don't use the tab button or indents, blah blah.. Heres what I tried:我为缺少代码表示歉意,但我使用的是苹果 iPad,他们不使用标签按钮或缩进,等等等等。这是我尝试过的:

[..omitted..]
var item = "null";
function executeFunction(){
    switch(item){
    case "item1":
        document.getElementById("div1").style.display='block';
        document.getElementById("div2").style.display='none';
        document.getElementById("div3").style.display='none';
        document.getElementById("div4").style.display='none';
        break;
    case "item1":
        document.getElementById("div1").style.display='block';
        document.getElementById("div2").style.display='none';
        document.getElementById("div3").style.display='none';
        document.getElementById("div4").style.display='none';
        break;
    case "item2":
        document.getElementById("div1").style.display='none';
        document.getElementById("div2").style.display='block';
        document.getElementById("div3").style.display='none';
        document.getElementById("div4").style.display='none';
        break;
    case "item3":
        document.getElementById("div1").style.display='none';
        document.getElementById("div2").style.display='none';
        document.getElementById("div3").style.display=block';
        document.getElementById("div4").style.display='none';
        break;
    case "item4":
        document.getElementById("div1").style.display='none';
        document.getElementById("div2").style.display='none';
        document.getElementById("div3").style.display='none';
        document.getElementById("div4").style.display='block';
        break;
    }
}

</script>
<ul>
    <li onClick="item = 'item1'; executeFunction();">Some item</li>
    <li onClick="item = 'item2'; executeFunction();">Some other item</li>
    <li onClick="item = 'item3'; executeFunction();">Some flying reptile</li>
    <li onClick="item = 'item4'; executeFunction();">Some psilocin</li>
</ul>
[..omitted..]

OK, first multiple commands can be used, separated by semicolons; OK,首先可以使用多个命令,用分号隔开; However, it is generally not recommended ... you might try creating a master function or a series of them so that only one function has to be mentioned in the HTML code.但是,通常不建议这样做……您可以尝试创建一个主函数或一系列主函数,以便在 HTML 代码中只需要提及一个函数。

Then, you don't need to create the variables ... try this然后,你不需要创建变量......试试这个

<li onClick="executeFunction('item1');">Some item</li>

... then, the executeFunction would be like this ... ...然后, executeFunction将是这样的 ...

function executeFunction(item) {
  document.getElementById("div1").style.display='none';
  document.getElementById("div2").style.display='none';
  document.getElementById("div3").style.display='none';
  document.getElementById("div4").style.display='none';

  switch(item){
    case "item1":
      document.getElementById("div1").style.display='block';
      break;
    case "item2":
      document.getElementById("div2").style.display='block';
      break;
    case "item3":
      document.getElementById("div3").style.display='block';
      break;
    case "item4":
      document.getElementById("div4").style.display='block';
      break;
  }
}

做: onClick="var='X'; doThis()"。

用分号分隔 onClick 中的 javascript 命令。

简单地:

 onclick="varname = value"

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

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