简体   繁体   English

读出JavaScript onClick函数体并将其用于另一个onClick

[英]Read out JavaScript onClick function body and use it for another onClick

I try to copy an onClick function from an image to an span object. 我尝试将onClick函数从图像复制到span对象。 But I don't get it. 但我不明白。

I have tried it directly with onClick=img.onClick , onClick=new Function(img.onClick) and more. 我已经用onClick=img.onClickonClick=new Function(img.onClick)等直接尝试了它。

if (img.onclick != undefined)
{
    var imgClick = img.onclick;
    strNewHTML = strNewHTML + " onMouseOver=\"this.style.background"
    + "= '#cecece'\" onMouseOut=\"this.style.background = ''\" onclick=\""+imgClick+"\">";
}

Can anyone help me? 谁能帮我?

Thanks! 谢谢!

span.onclick= img.onclick;

JavaScript is case-sensitive and DOM event handler properties are all lower-case. JavaScript区分大小写,DOM事件处理程序属性都是小写的。

edit: 编辑:

if (img.onclick != undefined) {
    strNewHTML = strNewHTML + " onMouseOver=\"this.style.background"
    + "= '#cecece'\" onMouseOut=\"this.style.background = ''\" onclick=\""+imgClick+"\">";
}

Well that's a completely different thing. 那是完全不同的事情。 You're creating an HTML string. 您正在创建HTML字符串。 But the onclick DOM property contains a function object. 但是onclick DOM属性包含一个function对象。 function objects can't be added into strings. function对象不能添加到字符串中。 (They would get converted to what you get if you call somefunction.toString() , which is not something that will work as an event handler.) (如果你调用somefunction.toString() ,它们会被转换为你得到的东西,这不会起到事件处理程序的作用。)

If you wanted to fetch the textual value of the onclick attribute to add into HTML, you'd have to do it with the span.getAttribute('onclick') method. 如果要获取要添加到HTML中的onclick属性的文本值,则必须使用span.getAttribute('onclick')方法执行此操作。 But that won't work in IE due to bugs in its implementation of getAttribute , so you'd have to resort to span.getAttributeNode('onclick').value . 由于getAttribute实现存在缺陷,因此无法在IE中使用,因此您必须使用span.getAttributeNode('onclick').value And then when you added it into the HTML string, you'd have to HTML-escape it, so that any < , & and " characters in it came out as &lt; etc., otherwise they'll break the markup. 然后当你将它添加到HTML字符串中时,你必须对其进行HTML转义,以便其中的任何<&"字符出现为&lt;等等,否则它们将破坏标记。

However, this is really ugly; 但是,这真的很难看; don't do it . 不要这样做 In reality, HTML string-slinging invariably sucks. 实际上,HTML字符串吊索总是很糟糕。 Especially when you've got JavaScript code inside HTML inside a JavaScript string. 特别是当您在JavaScript字符串中的HTML内部获得JavaScript代码时。 The escaping rules get insane and if you make a mistake escaping content that comes from user input, you've given yourself a cross-site-scripting security hole. 逃避规则变得疯狂,如果您在转发来自用户输入的内容时出错,那么您已经给自己一个跨站点脚本的安全漏洞。

Instead, use DOM methods. 相反,使用DOM方法。 This takes all the escaping out of the equation and it's generally more readable than hacked-together HTML markup strings. 这使得所有的转义都脱离了等式,并且它通常比被黑客攻击的HTML标记字符串更具可读性。 Then you can freely assign onclick to whatever function you like. 然后你可以自由地分配onclick到你喜欢的任何功能。 eg.: 例如。:

var span= document.createElement('span');
if (img.onclick)
    span.onclick= img.onclick;
span.onmouseover= function() {
    this.style.background= '#CECECE';
};
span.onmouseout= function() {
    this.style.background= '';
};

someparentelement.appendChild(span);

Also consider replacing the mouseover/mouseout with a simple CSS :hover rule, for maintainability. 还可以考虑使用简单的CSS :hover规则替换mouseover / mouseout,以实现可维护性。 The only browser that still needs help with :hover is IE6. 唯一仍需要帮助的浏览器:hover是IE6。

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

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