IE7 的 onclick setAttribute 解决方法

发布于 2024-11-15 07:18:53 字数 626 浏览 7 评论 0 原文

我似乎无法理解这一点:

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 中工作,我已经尝试了所有可以在网上找到的解决方法,想知道是否有人可以提供帮助?

I can't seem to get this:

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>');");

To work in IE7, I have tried all of the workarounds I could find online and wondering if anyone could help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

森林很绿却致人迷途 2024-11-22 07:18:53

不要那样做。

相反,通过调用 attachEvent / 添加事件处理程序 >添加事件监听器。

Don't do that.

Instead, add an event handler by calling attachEvent / addEventListener.

忆悲凉 2024-11-22 07:18:53

我知道我有点晚了,但这也让我很烦恼,但我终于明白了。

要让 IE 在 onclick 事件期间执行动态构建的代码,请执行以下操作:

  1. 为所有其他浏览器编写代码: element.setAttribute( "onclick", object.varName + "method( " + var + " )" );
  2. 使用自定义属性来保存动态语句: element.setAttribute( "exec", object.varName + "method( " + var + " )" );
  3. 创建一个匿名函数,以在单击元素时执行存储在自定义属性中的语句: element.onclick = function(){ eval( this.exec ); };

这种方法适用于在 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:

  1. Write the code for all other browsers: element.setAttribute( "onclick", object.varName + "method( " + var + " )" );
  2. Use a custom attribute to hold your dynamic statement: element.setAttribute( "exec", object.varName + "method( " + var + " )" );
  3. Create an anonymous function to execute the statement stored in our custom attribute when the element is clicked: element.onclick = function(){ eval( this.exec ); };

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.

源来凯始玺欢你 2024-11-22 07:18:53

当我这样做时,就像做梦一样:

if(navigator.userAgent.indexOf("MSIE 7.0") != -1){

    document.getElementById("pb").onclick = function(){ eval( this.onClick ); };
}

这样只有 ie7 才能看到它。

worked like a dream when I did this:

if(navigator.userAgent.indexOf("MSIE 7.0") != -1){

    document.getElementById("pb").onclick = function(){ eval( this.onClick ); };
}

that way only ie7 looks at it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文