Javascript 使用 JQuery 获取 onclick() 事件的字符串值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5422040/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 17:08:55  来源:igfitidea点击:

Using JQuery to get string value of an onclick() event

javascriptjquery

提问by Mark Kadlec

Wondered if there was good way to do this, thought I would post to the SO community...

想知道是否有好的方法可以做到这一点,以为我会发布到 SO 社区...

There is a 3rd party web page that I have no control over how it renders, but they allow me to add JQuery.

有一个 3rd 方网页,我无法控制它的呈现方式,但它们允许我添加 JQuery。

Using the JQuery, I am creating a nav menu on the side of the page, it will be a list of links. The onclick event of these links I get from existing onclick events already on the page, but when I do a:

使用 JQuery,我在页面一侧创建了一个导航菜单,它将是一个链接列表。这些链接的 onclick 事件是从页面上已有的 onclick 事件中获得的,但是当我执行以下操作时:

var linkLoc = $('#theLink').attr("onclick");

linkLoc returns:

linkLoc 返回:

function onclick(event) {
  handleJumpTo("com.webridge.entity.Entity[OID[E471CB74A9857542804C7AC56B1F41FB]]", "smartform");
}

instead of what I would expect:

而不是我所期望的:

handleJumpTo("com.webridge.entity.Entity[OID[E471CB74A9857542804C7AC56B1F41FB]]", smartform");

I think JQuery is trying to get the event for binding, but I need the actual Javascript markupsince I'm creating the HTML dynamically. I guess I could substring the "function onclick(event) {" out, but seems kind of hacky.

我认为 JQuery 正在尝试获取绑定事件,但我需要实际的 Javascript 标记,因为我正在动态创建 HTML。我想我可以将 "function onclick(event) {" 子串出来,但似乎有点笨拙。

Any ideas of an elegant way I could get the onclick markup?

我可以获得 onclick 标记的优雅方式的任何想法?

采纳答案by Rocket Hazmat

The type of $('#theLink').attr("onclick")is a function, so you can just use that when you bind events to the links.

的类型$('#theLink').attr("onclick")是一个函数,因此您可以在将事件绑定到链接时使用它。

var linkLoc = $('#theLink').attr("onclick");
$('a#link1').live('click', linkLoc);

Example: http://jsfiddle.net/BdU6f/

示例:http: //jsfiddle.net/BdU6f/

You can also run other code in the click handler too, if you need:

如果需要,您也可以在单击处理程序中运行其他代码:

var linkLoc = $('#theLink').attr("onclick");
$('a#link1').live('click', function(e){
    // Code...
    linkLoc(e);
});

Example: http://jsfiddle.net/BdU6f/1/

示例:http: //jsfiddle.net/BdU6f/1/

回答by user2994381

$("#theLink")would return a jQuery object whereas $("#theLink")[0]would give a DOM object. This is a resson that $("#thelink")[0].getAttributeNode('onclick').valuewould work.

$("#theLink")会返回一个 jQuery 对象,而$("#theLink")[0]会给出一个 DOM 对象。这是一个有效的resson $("#thelink")[0].getAttributeNode('onclick').value

回答by Pointy

The "onfoo" attributes have values that are functions, not strings. The semantics of:

“onfoo”属性的值是函数,而不是字符串。语义:

<whatever onclick='code code code'>

are that the browser constructs a function object as if you had code that did this:

是浏览器构造了一个函数对象,就好像您有执行此操作的代码一样:

document.getElementById('whatever').onclick = new Function("event", "code code code");

Thus you don't really need the raw string, since you've got something better: the function itself, ready to be called. You can then bind it as a handler to other elements via JavaScript code, not HTML (which is really a better way to do things anyway). You're using jQuery, you say, so you can use the jQuery ".bind()" API to bind those functions to whatever elements you need.

因此你并不真正需要原始字符串,因为你有更好的东西:函数本身,准备好被调用。然后,您可以通过 JavaScript 代码将其作为处理程序绑定到其他元素,而不是 HTML(无论如何,这确实是一种更好的处理方式)。您说,您正在使用 jQuery,因此您可以使用 jQuery“.bind()”API 将这些函数绑定到您需要的任何元素。

You should also be aware that there are other ways of binding event handlers to elements, ways that will leave the "onfoo" attributes completely unset.

您还应该知道还有其他方法可以将事件处理程序绑定到元素,这些方法将使“onfoo”属性完全未设置。

回答by James

If I understand where you're going with this, you should be able to assign the returned onclick function straight through to the onclick of your new nav element...

如果我明白你要做什么,你应该能够将返回的 onclick 函数直接分配给你的新导航元素的 onclick ......

$('#NewNavElement').click($('#theLink').attr('onclick'));

If you need to add additional code to the handler, you can just bind another click handler.

如果您需要向处理程序添加额外的代码,您可以绑定另一个点击处理程序。

回答by Brian Scott

try this;

尝试这个;

$('#theLink').getAttributeNode('onclick').value

Revised as per comment:

根据评论修改:

$('#theLink').get().getAttributeNode('onclick').value