IE7 的 onclick setAttribute 解决方法
我似乎无法理解这一点:
top.document.getElementById("clickThis").setAttribute("onclick", "tinyMCE.execCommand('mceInsertContent',false,'<div style=\"width: 600px; margin: 0 auto .5em;\" class=\"wp-caption alignnone\"><a href=\"<?php echo $full_image_path; ?>\" rel=\"lightbox\" title=\"View in lightbox\"><img class=\"alignnone\" src=\"<?php echo $full_width; ?>\" alt=\"<?php echo $value; ?>\" /></a><p class=\"wp-caption-text\"><?php echo $get_image->caption; ?></p></div>');");
为了在 IE7 中工作,我已经尝试了所有可以在网上找到的解决方法,想知道是否有人可以提供帮助?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不要那样做。
相反,通过调用
attachEvent
/添加事件处理程序 >添加事件监听器。
Don't do that.
Instead, add an event handler by calling
attachEvent
/addEventListener
.我知道我有点晚了,但这也让我很烦恼,但我终于明白了。
要让 IE 在 onclick 事件期间执行动态构建的代码,请执行以下操作:
这种方法适用于在 IE7 浏览器模式下运行的 IE9,以及当前版本的 Chrome 和 Firefox。
有几点需要注意:
首先,就我而言,我需要执行一个对象方法。为此,我需要知道对象的 var 名称,我在启动类时将其设置为属性。因此,object.varName 属性是我在运行时代码中创建并设置的属性。
其次,“exec”不是标准属性,这就是为什么它可以保存我想要执行的字符串。但只要使用非标准属性,您就可以随意称呼它。
第三,这就是为什么它有效:IE,或者至少是 IE7,只允许您将 onclick 事件设置为包含匿名函数。如果您分配一个对象的方法或变量包含
一个函数,该函数在设置 onclick 属性时执行,而不是在实际单击元素时执行。就我而言,我无法构建匿名函数,因为变量在运行时发生变化。因此,我动态地构建一个语句,然后将其存储在元素的属性中。然后 onclick 事件执行该属性中存储的任何语句。
干杯。
I know I'm a bit late, but this was bugging the hell out of me too, and I finally got it.
To get IE to execute dynamically built code during the onclick event, do this:
This approach is working for me in IE9 running in IE7 browser mode, as well as the current versions of Chrome and Firefox.
A couple of things to note:
First, in my case, I needed to execute an objects method. To do that, I need to know the var name for the object, which I set as a property when initiating the class. So the object.varName property is one I created and set in my runtime code.
Second, "exec" is not a standard attribute, which is why it works to hold the string I want to execute. But you can call it what ever you want as long as you use a non-standard attribute.
Third, here's why this works: IE, or at least IE7, will only let you set the onclick event to contain an anonymous function. If you assign an object's method or a variable containing
a function, the function is executed when the onclick attribute is set instead of when the element is actually clicked. In my case, I couldn't build an anonymous function because the variables change at runtime. So instead, I dynamically build a statement that is then stored in an attribute of the element. The onclick event then executes whatever statement is stored in that attribute.
Cheers.
当我这样做时,就像做梦一样:
这样只有 ie7 才能看到它。
worked like a dream when I did this:
that way only ie7 looks at it.