English 中文(简体)
在html中,在不放任何 j子的情况下寻找点击元素
原标题:Get id of clicked element without putting any js code in the html

我需要找到一个被点击的元件,但我需要把我的 j和html分开。 通常只是这样做。 html。 任何其他方式?

最佳回答

这就是<代码>e.target。 只要可以把点击事件遮掩起来,你就可以在你想要的任何地方增加一个活动听众。 基本例子:

document.addEventListener( click , function(e) {
    alert(e.target.id);
});

每一次点击都将警示。 希望:

<button id="foo">foo</button>
<button id="bar">bar</button>
<button id="baz">baz</button>

http://www.ohchr.org。

http://jsfiddle.net/95NZ8/

问题回答

类似情况:

$(document).ready(function () {
    $("#someWrapper").each(function () {
        $(this).on( click , function () {
            alert(this.id);
        });
    });
});




相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签