简体   繁体   English

将内联onclick事件与jquery onclick事件组合

[英]Combining inline onclick event with jquery onclick event

I have some code that has inline onclick events, and that code cannot be changed. 我有一些具有内联onclick事件的代码,并且该代码无法更改。

The code does 该代码确实

onclick="javascript:document.myform.hiddenfield.value='1234';
document.myform.submit(); return false;"

Now, I want to intercept and modify this with an external jQuery file. 现在,我想使用外部jQuery文件进行拦截和修改。

However, I have found that the inline onclick will run before any attached event through jQuery. 但是,我发现内联onclick将在通过jQuery发生任何附加事件之前运行。

So my idea was to do something onload, like fetch the inline onclick text, write it to a new attribute for the link, say oldonclick, then set onclick to null via $.prop() 所以我的想法是做一些加载,例如获取内联onclick文本,将其写入链接的新属性,例如oldonclick,然后通过$ .prop()将onclick设置为null。

Then my event handler can do it's thing, and if the requirements are met, actually read and execute the old value. 然后,我的事件处理程序可以执行此操作,如果满足要求,则实际读取并执行旧值。

However, at the first hurdle I get a bit stuck, $.prop("onclick") returns a function, not the string from onclick. 但是,首先遇到困难,$。prop(“ onclick”)返回一个函数,而不是onclick的字符串。 Therefore I can't assign it to some dummy value and move to the next step. 因此,我无法将其分配给某个虚拟值并转到下一步。

How can I read the onclick as text, and how could I achieve this jQuery intercept some other way? 如何将onclick读取为文本,以及如何通过其他方式实现jQuery拦截?

Try this: 尝试这个:

var onclick = $("a").attr("onclick"); //This is a string

Check the fiddle: https://jsfiddle.net/p50keLy6/ 检查小提琴: https : //jsfiddle.net/p50keLy6/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM