jQuery方法在IE上失败 [英] jQuery method fails on IE
问题描述
我有这种方法:
function replaceRightClickIcefacesMethod() {
var oldName = jQuery(".singlePaneOfGlassBlock").attr("oncontextmenu");
oldName = oldName.replace('Ice.Menu.contextMenuPopup', 'contextMenuPopupUpdated');
alert(oldName);
jQuery(".singlePaneOfGlassBlock").attr("oncontextmenu", oldName);
}
在Chrome或FF上效果很好. 但是在IE上,我收到此抱怨:
which works nice on Chrome or FF. BUT on IE I receive this complaining:
对象不支持此属性或方法
Object does not support this property or method
它使我指向第三行.
您看到任何解决方法了吗?
Do you see any work-around?
Ps:我正在使用最新版本的jQuery(1.6)
Ps: I'm using latest version of jQuery (1.6)
更新:
我也尝试过:
var oldName = jQuery(".singlePaneOfGlassBlock")[0].getAttribute('oncontextmenu');
但对于IE来说仍然是相同的问题
but still the same problem for IE
推荐答案
这是IE 8之前的Internet Explorer版本的问题.attr()
映射到getAttribute
作为事件处理程序,但是较旧的IE存在导致getAttribute
仅返回DOM属性值而不是属性字符串.
This is a problem with Internet Explorer versions before IE 8. attr()
maps to getAttribute
for event handlers, but older IEs had a bug which caused getAttribute
to just return the DOM property value instead of the attribute string.
除了在IE中解析outerHTML
字符串(您真的 不想这样做:-)
I can't think of a way around it, save for parsing the outerHTML
string in IE, which you really don't want to do :-)
对于所有浏览器而言,最好的方法是首先使用jQuery bind
进行事件处理:
The best approach, for all browsers, is to bind
to the event using jQuery in the first place:
$(".singlePaneOfGlassBlock").bind("contextmenu", contextMenuPopupUpdated);
然后在需要时切换到其他功能(unbind
,然后再次bind
):
And then swap to a different function (unbind
, then bind
again) when you need to:
function replaceRightClickIcefacesMethod() {
$(".singlePaneOfGlassBlock").unbind().bind("contextmenu", function () {
Ice.Menu.contextMenuPopup();
});
}
您已经发现,可以使用getAttributeNode()
来获取节点的字符串值.为了设置属性,必须在分配字符串之前从字符串创建一个函数.一个简单的方法可能是:
As you figured out, you can use getAttributeNode()
to get the string value of the node. In order to set the attribute, you have to create a function from the string before assigning it. A simple approach to this might be:
function replaceRightClickIcefacesMethod() {
var elem = jQuery(".singlePaneOfGlassBlock"),
oldName = elem.attr("oncontextmenu"),
fn = String;
if (typeof oldName == "function")
oldName = elem[0].getAttributeNode("oncontextmenu").value,
fn = Function;
oldName = oldName.replace('Ice.Menu.contextMenuPopup', 'contextMenuPopupUpdated');
alert(oldName);
elem[0].setAttribute("oncontextmenu", fn(oldName));
}
如果原始类型是字符串,则将字符串传递给String
,这将没有任何实际效果,但是如果原始类型是函数,则在将其设置为新字符串之前,将所得字符串传递给Function
.属性的值.
This passes the string to String
if the original type is a string, which will have no real effect, but if the original type is a function, the resulting string is passed to Function
before being set as the new value for the attribute.
这篇关于jQuery方法在IE上失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!