使用 Javascript 更改 OnClick 事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18759721/
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
Change the OnClick event using Javascript
提问by Shadow
I have an HTML anchor tag like
我有一个 HTML 锚标记,如
<a href="anchorid" onclick="callEvent(1)">
Here I call the Javascript function like
在这里,我将 Javascript 函数称为
<script>
function callEvent(anchor) {
alert("Anchor ID is - "+anchor);
document.getElementById("anchorid").onClick = function () { callEvent(0) }; // BY using this code, update the onclick callEvent(0), like toggle
}
</script>
I wants to update the anchor tag like
我想更新锚标签
<a href="anchorid" onclick="callEvent(0)">
When using this code, it is not updating as per my requirement.
使用此代码时,它不会按照我的要求进行更新。
document.getElementById("anchorid").onClick = function () { callEvent(0) };
How do I get it to update?
我如何让它更新?
回答by Sudhir Bastakoti
for using document.getElementById("anchorid")..
you need to have id
in your element, which you currently dont have, try doing:
为了使用document.getElementById("anchorid")..
你需要id
在你的元素中拥有你目前没有的元素,请尝试执行以下操作:
//add id to your anchor
<a href="javascript:void(0);" id="anchorid" onclick="return callEvent(1);">
test
</a>
and js
和js
<script type="text/javascript">
function callEvent(num) {
alert("Anchor ID is - "+num);
document.getElementById('anchorid').onclick = function () { callEvent(0) };
}
</script>
回答by Akshay
Store the toggle value in tag itself and then use.
将切换值存储在标签本身中,然后使用。
<script>
function callEvent(val) {
var val = document.getElementById("myId").getAttribute('data-val');
alert(val);
// toggle value
val = val==1?0:1;
document.getElementById("myId").setAttribute('data-val',val);
}
</script>
<a id="myId" data-val="0" onClick="callEvent()">Click</a>
here value is stored in data-val
value so that is toggled in the callEvent function itself, so no need to rebind the event.
这里的 value 存储在data-val
value 中,以便在 callEvent 函数本身中切换,因此无需重新绑定事件。
See Example fiddle
回答by Dexa
if you only change parameter of calling function then you dont have to change complete event. You can do it like this:
如果您只更改调用函数的参数,则不必更改完成事件。你可以这样做:
HTML
HTML
<a id="anchorid" href="#">click me !</a>
JS
JS
<script>
var anchor = 1;
document.getElementById("anchorid").onclick = function(){
alert("Anchor ID is: " + anchor);
anchor = anchor == 1 ? 0 : 1; //toggle 0 and 1
}
<script>
回答by Shadow
try this, it may solve your problem demoFiddle
试试这个,它可能会解决你的问题演示小提琴
Add id="anchorid" to your anchor tag
将 id="anchorid" 添加到您的锚标签
When you click it next time it will callEvent by argument 0,
当您下次单击它时,它将通过参数 0 callEvent,
function callEvent(anchor) {
alert("Anchor ID is - "+anchor);
var ele = document.getElementById("anchorid");
ele.setAttribute("onclick","callEvent(0)");
}
It will update your link like you wanted
它会像你想要的那样更新你的链接
<a href="anchorid" id="anchorid" onclick="callEvent(0)">
回答by karaxuna
try:
尝试:
document.getElementById("anchorid").onclick
Better:
更好的:
document.getElementById("anchorid").addEvenetListener('click', function(){
});
回答by Dardino
Tested: http://jsfiddle.net/Yca5W/
测试:http: //jsfiddle.net/Yca5W/
document.getElementById("anchorid").onclick =
function () {
alert("clicked");
};
回答by ratnesh dwivedi
try This:
试试这个:
var anchor= document.getElementById('anchorid');
anchor.addEventListener('click', function() {
callEvent(0);
});
OR
或者
$( "#anchorid" ).click(function() {
callEvent(0);
});
if you only want changes passing parameter from 1 to 0 or vice verse then do this one:
如果您只想将参数从 1 更改为 0 或反之亦然,请执行以下操作:
<input type="hidden" name="para" id="para" value="1" >
<a href="anchorid" onclick="callEvent($('#para').val())">
$( "#anchorid" ).click(function() {
if ($('#para').val()==1) {
$('#para').val(0)
} else {
$('#para').val(1)
}
});