$("input[type='button'][onclick^='save']")
$(“输入(type = '按钮'][onclick ^ = '保存']”)
and it works in FF but on IE...
它在FF中起作用,但是在IE中。
There is something wrong with onclick selector part. Is there a way to make cross-browser workaround?
onclick选择器部分有问题。有没有办法让跨浏览器工作?
Thanks Pawel
由于帕维尔
edit:
编辑:
$("img[src$='scroll.gif']").click(function(){
var targetOffset = $("input[type='button'][onclick^='save']").offset().top;
$("html,body").animate({scrollTop: targetOffset}, 400);
});
4 个解决方案
#1
1
You can create a custom selector (probably very inefficient though):
您可以创建一个自定义选择器(可能非常低效):
$.extend($.expr[':'], {
onclick: function(node, index, args, stack) {
var events = $(node).data('events');
var fn = args[3];
if (!events || !events.click) return false;
for (i in events.click) {
if (events.click[i].name == fn) return true;
}
return false;
}
});
$("input[type='button']:onclick('save')")
function.name
does not work in IE though, so you will have to jump through another hook using toString() and regexps. All in all, you are far better off manually maintaing a list of event handler names.
虽然在IE中函数名不起作用,所以您必须使用toString()和regexp跳过另一个钩子。总而言之,最好手工维护事件处理程序名称列表。
Edit: looking at your code snippet, there seems to be no reason at all to search for event handlers. Instead of wildly throwing attribute selectors around, use classes and ids to address your elements in a semantically meaningful way, e.g.
编辑:查看您的代码片段,似乎完全没有理由去搜索事件处理程序。不要到处抛出属性选择器,使用类和id以语义上有意义的方式处理元素,例如。
<img id="scroll-icon" src="icons/scroll.gif" />
<input id="submit-button" type="submit" onclick="save();" />
and then
然后
$("#scroll-icon".click(function(){
var targetOffset = $("submit-buttton").offset().top;
$("html,body").animate({scrollTop: targetOffset}, 400);
});
#2
6
It should be like :
应该是这样的:
$("input[type=button]").click(function(){
//do something
});
You should put this in document ready function
您应该将其放入文档就绪函数中。
EDIT is this what you want?
编辑这是你想要的吗?
$('img[src="scroll.gif"]').click(function(){
var targetOffset = $(this).offset().top;
$("html,body").animate({scrollTop: targetOffset}, 400);
});
#3
3
Do you have more than one button that is a save button? If not, why don't you just assign your button an id? That makes it a lot simpler, and the id selector, which wraps document.getElementById()
is very cross-platform. Then, all you need is $("#buttonID")
.
是否有多个按钮是保存按钮?如果没有,为什么不给按钮分配一个id呢?这使它更加简单,而id选择器(用于包装document.getElementById()))是非常跨平台的。然后,您需要的是$(“#buttonID”)。
EDIT: Given the OP's criteria, I'd suggest a class (just for use as a selector) such as "jumpToSave". Each button should have that class, and then the selector could use $(".jumpToSave")
编辑:考虑到OP的标准,我建议一个类(仅用作选择器),例如“jumpToSave”。每个按钮都应该有这个类,然后选择器可以使用$(“.jumpToSave”)
#4
2
You can't look for a value like that in event attributes. The browser creates a function from the string value in the attribute.
不能在事件属性中查找这样的值。浏览器从属性中的字符串值创建一个函数。
If you have an attribute like this:
如果你有这样的属性:
onclick="save();"
The attribute contains a function reference rather than a string. If you read the attribute and get the value as a string, you get the code of the function.
该属性包含一个函数引用,而不是字符串。如果您读取该属性并将值作为字符串获取,您将获得该函数的代码。
When reading the attribute value in Firefox, this is what you get:
当读取Firefox中的属性值时,会得到以下结果:
function onclick(event) { save(); }
When reading the attribute in Internet Explorer, this is what you get:
在Internet Explorer中读取属性时,会得到以下结果:
function onclick(){save();}
You have to use some other attribute to identify the button.
您必须使用其他一些属性来标识按钮。
#1
1
You can create a custom selector (probably very inefficient though):
您可以创建一个自定义选择器(可能非常低效):
$.extend($.expr[':'], {
onclick: function(node, index, args, stack) {
var events = $(node).data('events');
var fn = args[3];
if (!events || !events.click) return false;
for (i in events.click) {
if (events.click[i].name == fn) return true;
}
return false;
}
});
$("input[type='button']:onclick('save')")
function.name
does not work in IE though, so you will have to jump through another hook using toString() and regexps. All in all, you are far better off manually maintaing a list of event handler names.
虽然在IE中函数名不起作用,所以您必须使用toString()和regexp跳过另一个钩子。总而言之,最好手工维护事件处理程序名称列表。
Edit: looking at your code snippet, there seems to be no reason at all to search for event handlers. Instead of wildly throwing attribute selectors around, use classes and ids to address your elements in a semantically meaningful way, e.g.
编辑:查看您的代码片段,似乎完全没有理由去搜索事件处理程序。不要到处抛出属性选择器,使用类和id以语义上有意义的方式处理元素,例如。
<img id="scroll-icon" src="icons/scroll.gif" />
<input id="submit-button" type="submit" onclick="save();" />
and then
然后
$("#scroll-icon".click(function(){
var targetOffset = $("submit-buttton").offset().top;
$("html,body").animate({scrollTop: targetOffset}, 400);
});
#2
6
It should be like :
应该是这样的:
$("input[type=button]").click(function(){
//do something
});
You should put this in document ready function
您应该将其放入文档就绪函数中。
EDIT is this what you want?
编辑这是你想要的吗?
$('img[src="scroll.gif"]').click(function(){
var targetOffset = $(this).offset().top;
$("html,body").animate({scrollTop: targetOffset}, 400);
});
#3
3
Do you have more than one button that is a save button? If not, why don't you just assign your button an id? That makes it a lot simpler, and the id selector, which wraps document.getElementById()
is very cross-platform. Then, all you need is $("#buttonID")
.
是否有多个按钮是保存按钮?如果没有,为什么不给按钮分配一个id呢?这使它更加简单,而id选择器(用于包装document.getElementById()))是非常跨平台的。然后,您需要的是$(“#buttonID”)。
EDIT: Given the OP's criteria, I'd suggest a class (just for use as a selector) such as "jumpToSave". Each button should have that class, and then the selector could use $(".jumpToSave")
编辑:考虑到OP的标准,我建议一个类(仅用作选择器),例如“jumpToSave”。每个按钮都应该有这个类,然后选择器可以使用$(“.jumpToSave”)
#4
2
You can't look for a value like that in event attributes. The browser creates a function from the string value in the attribute.
不能在事件属性中查找这样的值。浏览器从属性中的字符串值创建一个函数。
If you have an attribute like this:
如果你有这样的属性:
onclick="save();"
The attribute contains a function reference rather than a string. If you read the attribute and get the value as a string, you get the code of the function.
该属性包含一个函数引用,而不是字符串。如果您读取该属性并将值作为字符串获取,您将获得该函数的代码。
When reading the attribute value in Firefox, this is what you get:
当读取Firefox中的属性值时,会得到以下结果:
function onclick(event) { save(); }
When reading the attribute in Internet Explorer, this is what you get:
在Internet Explorer中读取属性时,会得到以下结果:
function onclick(){save();}
You have to use some other attribute to identify the button.
您必须使用其他一些属性来标识按钮。