Home > Web Front-end > JS Tutorial > How to determine if jquery is a string

How to determine if jquery is a string

coldplay.xixi
Release: 2023-01-04 09:36:34
Original
3520 people have browsed it

Jquery method to determine whether it is a string: 1. Determine it as a string type, the code is [alert((typeof str=='string')&&str.constructor==String)]; 2. Determine it as Array type, the code is [alert((typeof..].

How to determine if jquery is a string

The operating environment of this tutorial: windows7 system, jquery3.2.1 version, Dell G3 computer, the The method is applicable to all brands of computers.

Recommendation: jquery video tutorial

jquery method to determine whether it is a string:

1. Determine whether it is a string type

var str="ss";
alert((typeof str=='string')&&str.constructor==String)
Copy after login

2. Determine whether it is an array type

var obj=[0];
alert((typeof obj=='object')&&obj.constructor==Array)
Copy after login

Method 2

function isString(obj){ //判断对象是否是字符串  
   return Object.prototype.toString.call(obj) === "[object String]";  
}  
验证:
var str1 = 'abc';
var str2 = new String('abc');
 
typeof str1; //"string"
typeof str2; //"object"
Object.prototype.toString.call(str1); //"[object String]"
Object.prototype.toString.call(str2); //"[object String]"
Copy after login

3. Determine whether it is a numeric type

var str=547.97;
alert((typeof str=='number')&&str.constructor==Number)
Copy after login

4. Determine whether it is a date type

var obj =new Date();
alert((typeof obj=='object')&&obj.constructor==Date)
Copy after login

5. Determine whether it is a function

var obj = function test(){};
alert((typeof obj=='function')&&obj.constructor==Function)
Copy after login

Related free learning recommendations: javascript(Video)

The above is the detailed content of How to determine if jquery is a string. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template