简体   繁体   English

为什么jQuery选择器适用于Chrome而不适用于Safari?

[英]Why jQuery selector works on Chrome but not on Safari?

I have the following code: 我有以下代码:

 var l = $("input[name*=hello").length; $('#result').text(l); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div> <input name="hello" type="text"> </div> <div> <input name="hello" type="text"> </div> <div id="result"> </div> 

Where the jQuery selector is: jQuery选择器在哪里:

input[name*=hello

Check that the selector is not correctly written input[name*=hello since a ] is missing. 检查选择器是否未正确写入input[name*=hello因为a ]缺失。

On Chrome I get on the div[id=result] the amount of inputs that I have with the name "hello" (2). 在Chrome上,我得到了div [id = result]我输入的名称为“hello”(2)的输入量。

But on Safari I get an error on the Console: 但在Safari上我在控制台上收到错误:

在此输入图像描述

Which is correct, so the question is: Why did Chrome "autocorrect" the selector? 这是正确的,所以问题是:为什么Chrome“自动更正”选择器? Is it possible to remove that behaviour? 是否有可能消除这种行为?

If the selector doesn't contain any jQuery extensions, jQuery will use the browser's built-in document.querySelectorAll() function. 如果选择器不包含任何jQuery扩展,jQuery将使用浏览器的内置document.querySelectorAll()函数。 When the selector is invalid, the behavior is unspecified, so it may differ between browsers. 当选择器无效时,行为未指定,因此浏览器之间可能不同。 Some browsers try to "fix" the selector and return something useful, while other browsers signal an error. 有些浏览器试图“修复”选择器并返回有用的东西,而其他浏览器则发出错误信号。 You shouldn't depend on this -- write correct selectors. 你不应该依赖于此 - 写出正确的选择器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM