简体   繁体   中英

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:

input[name*=hello

Check that the selector is not correctly written input[name*=hello since a ] is missing.

On Chrome I get on the div[id=result] the amount of inputs that I have with the name "hello" (2).

But on Safari I get an error on the Console:

在此输入图像描述

Which is correct, so the question is: Why did Chrome "autocorrect" the selector? 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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