Onclick函数基于元素id [英] Onclick function based on element id

查看:114
本文介绍了Onclick函数基于元素id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个具有继承关系可视化的网站。

I'm working on a site with a visualization of inheritance relation.

我尝试将每个节点框与特定的URL链接起来。 Element的html如下所示:

I try to link each nodebox with specific URLs. The html of Element looks like this:

<g class="node" id="RootNode" transform="translate(0,453.125)">

我用过

$('#RootNode').click(function(){//do something}

并且还

document.getElementById("RootNode").onclick(){//do something}

他们都没有找到元素,也没有设置onclick功能。

Neither of them can find the element, nor setup the onclick function.

我误解了什么吗?任何信息都会有所帮助。谢谢。

Have I misunderstood anything? Any information will be helpful. Thanks.

推荐答案

确保您的代码在< a href =http://learn.jquery.com/using-jquery-core/document-ready/ =noreferrer> DOM Ready ,如 rocket-hazmat

。click()

$('#RootNode').click(function(){
  //do something
});







document.getElementById("RootNode").onclick = function(){//do something}



。on()

使用活动代表团/

$(document).on("click", "#RootNode", function(){
   //do something
});



尝试


Try

在Dom Ready中包装代码

Wrap Code in Dom Ready

$(document).ready(function(){
    $('#RootNode').click(function(){
     //do something
    });
});

这篇关于Onclick函数基于元素id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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