admin管理员组文章数量:1421176
I have the follwing jQuery:
$("#textArea").keyup(function(){
var textAreaValue = $("textArea");
if(!textArea.value.indexOf("some string")){
textArea.value = textArea.value.replace("some string",null);
alert("It was there!");
}
});
Is it normal for
element.value.replace("some string",null);
to replace"some string"
with"null"
as a string? And if normal can you please explain why?I have tested it with
element.value.replace("some string","")
, and that works fine, so what would be the difference betweennull
and""
?
Using FireFox 3.6.3, Thanks in advance.
I have the follwing jQuery:
$("#textArea").keyup(function(){
var textAreaValue = $("textArea");
if(!textArea.value.indexOf("some string")){
textArea.value = textArea.value.replace("some string",null);
alert("It was there!");
}
});
Is it normal for
element.value.replace("some string",null);
to replace"some string"
with"null"
as a string? And if normal can you please explain why?I have tested it with
element.value.replace("some string","")
, and that works fine, so what would be the difference betweennull
and""
?
Using FireFox 3.6.3, Thanks in advance.
Share Improve this question edited Jun 16, 2010 at 4:12 Babiker asked Jun 16, 2010 at 4:07 BabikerBabiker 18.8k28 gold badges82 silver badges127 bronze badges3 Answers
Reset to default 3Seems like null has been type-casted to a String.
Try:
"a" + null // "anull"
Although you can't call toString() on a null
object, it seems the null object is implicitly being converted to a String, which gives the string "null"
.
String(null) // "null"
The second parameter of String.replace
is a required parameter, and it must be a string. See mdc and w3schools. It's not normal or safe to pass null
, which is not a string. Don't be surprised if your code does not execute properly in all javascript engines.
""
is an empty string..
null
is something that indicates a deliberate non-value or undefined...
null
is mostly used to initialize objects
and in str.replace(param1,param2), param1 and param2 should be a string
or something that produces a string
( in param2 )... in that said,
var heart_type = 'images/unheart.png';
alert( heart_type.replace(".png",null));
will alert images/unheartnull
.. because null was treated as a string
...
.replace()
reference
本文标签: jqueryPassing null as a param for replace() in javascript behaving weirdStack Overflow
版权声明:本文标题:jquery - Passing null as a param for replace() in javascript behaving weird? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745349249a2654661.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论