为什么Internet Explorer不能运行这个简单的jQuery? [英] Why can't Internet Explorer run this simple jQuery?
问题描述
我正在运行一个非常简单的jQuery脚本来获取表格中复选框选中的所有电子邮件。该表如下所示:
I'm running a really simple jQuery script to grab all emails selected by checkboxes in a table. The table looks like this:
<table>
<tbody>
<tr>
<td>
<input type="checkbox" value="MD5HASH" />
</td>
<td>
First Name
</td>
<td class="email">
Email Address
</td>
</tr>
</tbody>
</table>
我的jQuery看起来像这样:
My jQuery looks like this:
$("#submitButton").click(function() {
var output = [];
$("table tbody tr:has(input:checkbox:checked)").each(function() {
var email = $('td.email', $(this)).text();
if (validate(email) && output.indexOf(email) == -1)
output.push(email);
});
$("#emails").val(output.join(", "));
});
function validate(email) {
return /[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/.test(email);
}
这在IE中惨遭失败,但在其他地方都有效。
This fails miserably in IE, but works everywhere else.
-
表tbody tr:has(输入:复选框:选中)
选择器不匹配。 - 对validate的调用会抛出
Object expected
错误。
- The
table tbody tr:has(input:checkbox:checked)
selector matches nothing. - The call to validate throws a
Object expected
error.
为什么!?是不是jQuery设计为跨浏览器和可移植?
WHY!? Isn't jQuery designed to be cross-browser and portable?
推荐答案
Internet Explorer(< 9)没有< a href =https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/indexOf =nofollow> Array.prototype.indexOf
。尝试使用jQuery的 $。inArray
代替(它是跨浏览器,实际上将使用 Array.prototype.indexOf
如果它存在:-P)。
Internet Explorer (< 9) doesn't have Array.prototype.indexOf
. Try using jQuery's $.inArray
instead (it's cross browser and will actually use Array.prototype.indexOf
if it exists :-P).
if (validate(email) && $.inArray(email,output) == -1)
output.push(email);
这篇关于为什么Internet Explorer不能运行这个简单的jQuery?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!