Javascript 如何使用javascript更改onclick?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42824309/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change onclick using javascript?
提问by mongkon mamiw
How to change onclick using javascript ?
如何使用 javascript 更改 onclick ?
https://jsfiddle.net/749rt0m7/1/
https://jsfiddle.net/749rt0m7/1/
I tried to use this code, but not work. How can i do ?
我尝试使用此代码,但不起作用。我能怎么做 ?
<span onclick="test_fn('aaa,bbb')" id="xx">edit</span>
<script>
document.getElementById('xx').onclick = "test_fn(111,222)";
</script>
回答by Alesana
There are a couple things going on here.
这里有几件事情正在发生。
First of all test_fn()is not a function in your jsfiddle. You will need to define it in order for it to be called.
首先test_fn()不是 jsfiddle 中的函数。您需要定义它才能调用它。
Second of all you are setting the onclick attribute to be a string not a function. You would have to set it to be a function using the code below.
其次,您将 onclick 属性设置为字符串而不是函数。您必须使用以下代码将其设置为函数。
document.getElementById('xx').onclick = function() { test_fn(111,222) };
Here is an example of a working jsfiddle.
这是一个有效的 jsfiddle 示例。
UPDATE
更新
It looks like I misunderstood the question. If you would actually like to change the onclick attribute, as in, change what the HTML shows, you will want to use setAttribute(). So your code would be..
看来我误解了这个问题。如果您真的想更改 onclick 属性,例如更改 HTML 显示的内容,您将需要使用setAttribute(). 所以你的代码将是..
document.getElementById('xx').setAttribute( "onClick", "test_fn('111,222')" );
Here is the updated jsfiddle
As a side note, like nnnnnnsays in a comment of the original post, using this you are passing 1 parameter through the function which is the string 111,222when it seems that you might want to be passing two. If you would like to pass 111as the first parameter and 222as the second parameter, you would call the function with either test_fn(111,222)(passing the parameters as integers) or with test_fn('111','222')(passing the parameters as strings)
作为旁注,就像 nnnnnn在原始帖子的评论中所说的那样,使用它,您将通过函数传递 1 个参数,该函数是字符串,111,222当您似乎想要传递两个参数时。如果您想111作为第一个参数和222第二个参数test_fn(111,222)传递,您可以使用(将参数作为整数test_fn('111','222')传递)或使用(将参数作为字符串传递)调用该函数
回答by Kyle Lin
document.getElementById('xx').onclick = function() {
test_fn(111, 222);
};
When setting onclickin HTML, use a string. When setting it through JS, use a function.
onclick在 HTML 中设置时,请使用字符串。通过JS设置的时候,使用一个函数。
回答by hackerrdave
You need to set onclickto a function, that runs your code test_fn('111,222'):
您需要设置onclick一个运行您的代码的函数test_fn('111,222'):
<script>
document.getElementById('xx').onclick = function() { test_fn('111,222'); }
</script>
回答by Ahmed Eid
I don't see where you are defining the function test_fnyou are hooking the function to the element onclicktwice in the html and in js .. only one is needed , and you need to actually define the function
我没有看到你在哪里定义函数,test_fn你onclick在 html 和 js 中两次将函数连接到元素..只需要一个,你需要实际定义函数
<span id="xx">edit</span>
<!-- or .. not both , they are doing pretty much the same thing-->
<script>
document.getElementById('xx').onclick = function(){
console.log('clicked')
}
</script>
now you need to define test_fnit will get called upon clicking the element
现在你需要定义test_fn它会在点击元素时被调用
function test_fn(arg1 , arg2){
condole.log(arg1 , arg2)
}
note that the html onclickversion is passing one argument and the js script version is passing two argumens.
请注意,htmlonclick版本传递一个参数,而 js 脚本版本传递两个参数。

