English 中文(简体)
如何把点击事件添加到一个要素中?
原标题:How to add click event to an element?

我愿在简便的Java(不使用jQuery)中添加一个点击事件,作为类似内容,因此我没有一个<代码>id,而是一个类别:

<a href="http://example.com/share" class="MyClass">Yummy</a>
最佳回答

如果你没有id子,也没有任何选修图书馆,而且你希望图书馆在老的浏览器上工作,那么它就只能做更多的工作。 如果你能够id,那就非常简单。 如果没有,则采取更多的法律:

var links = document.getElementsByClassName("MyClass");
links[0].onclick = function() {
    // put your click handling code here
    // return(false) if you don t want default click behavior for the link
}

Since getElementsByClassName is not universally available in older browsers, you would need a shim to implement it when not present. Or, you could get all the links in your document with:

var links = document.getElementsByTagName("a");

然后通过该名单,直到你找到你想要的人选(或许可以核对班子名称)。

如果你能够把该链接列入一个身份证:

<a href="http://braza.com/share" id="specialLink" class="MyClass" >Yummy</a>

然后,它只是采取这一法律:

document.getElementById("specialLink").onclick = function() {
    // add code here
}

如果你重新定期这样做,增加一名活动听众比使用浮雕财产要少一些,但如果你没有任何框架,那么你需要增加一个处理旧版本的电离层电离层电离层器的活动听众。

问题回答

可以采取几种方式这样做。

One is you add the click event right in the anchor

缩略语

另一种方式是使用文件。

如:document.getElementsByTagName(a)[0].click=职能(a) }

如果我知道你能够提供这一指数的阵列的确切位置,那就只是象征性的。

三种方式是,你可以撰写一种习俗。 文件。 抵达 内容 CNClassName在javascript中发挥作用,并且以西语使用。 你们可以找到一些执行工具。 ByClassName,ating google.

look at http://robertnyman.com/2005/11/07/the-ultimate-getelementsbyclassname/ one of the implementation.

You simple use like below

<a href="http://braza.com/share" class="MyClass" onclick= return somefunction() >Yummy</a>

<script>

function somefunction()
{
 // do your stuff.
// return true, if you want to open the link, or false to cancel
return true;
}

</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html>
<head>
<title>Untitled</title>
<style type="text/css">
td { border: 1px solid #ccc; }
.findMe { color: gold; }
.youFoundMe { color: green; }
</style>

<script type="text/javascript"><!--

var aryClassElements = new Array();

function doSomething() {
aryClassElements.length = 0;
getElementsByClassName(  findMe , document.body );
for ( var i = 0; i < aryClassElements.length; i++ ) {
    aryClassElements[i].className =  youFoundMe ;
}
}

function getElementsByClassName( strClassName, obj ) {
if ( obj.className == strClassName ) {
    aryClassElements[aryClassElements.length] = obj;
}
for ( var i = 0; i < obj.childNodes.length; i++ )
    getElementsByClassName( strClassName, obj.childNodes[i] );
}

//--></script> 
</head>

<body onload="doSomething();">
<h1>Heading 1</h1>
<div>
This code is inside my div.
<span>This code is inside a span inside the div. <a href="#" class="findMe">Link inside   the span inside the div.</a></span>
<a href="#">Link inside the div.</a>
</div>
<p>
<h2 class="findMe">My Paragraph s Heading 2</h2>
<table>
    <tr>
        <td class="findMe">My first cell.</td>
        <td>My second cell. <a href="#" class="findMe">Link inside the cell inside the  row inside the table.</a></td>
    </tr>
</table>
</p>
</body>
</html>`




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签