使用JavaScript无法更改onclick事件 [英] Change onclick event with JavaScript not working

查看:160
本文介绍了使用JavaScript无法更改onclick事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个:

function tp_visible(action){
    if(action==1){
        document.getElementById("tp").style.display='block';
        document.getElementById("tp_action").onclick='tp_visible(0);
        return false;';
    }
    else {
        document.getElementById("tp").style.display='none';
        document.getElementById("tp_action").onclick='tp_visible(1);
        return false;';
    }
    return false;
}

为什么上面没有更改 onclick 事件?

Why isn't the above changing the onclick event?

我使用 Firebug 和事件保持不变...

I use Firebug and the event remains the same...

这是HTML:

<a
    id='tp_action'
    name='tp_action'
    href='#'
    onclick='tp_visible(1);
    return false;'
>Show info</a>


推荐答案

下面的检查将为您工作,因为您无法分配

The check below will work for you because you cannot assign a string value to the handler onclick.

function tp_visible(action){
    if(action==1){
        document.getElementById("tp").style.display='block';
        document.getElementById("tp_action").onclick= function ()
            { tp_visible(0); return false;};
    }
    else {
        document.getElementById("tp").style.display='none';
        document.getElementById("tp_action").onclick= function ()
            {tp_visible(1); return false; }
    }
    return false;
}

这篇关于使用JavaScript无法更改onclick事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆