Javascript jQuery不包含';不要在铬上工作
我对jquery包含有一个问题。它在firefox上工作得非常好 这是我的密码Javascript jQuery不包含';不要在铬上工作,javascript,jquery,google-chrome,contains,Javascript,Jquery,Google Chrome,Contains,我对jquery包含有一个问题。它在firefox上工作得非常好 这是我的密码 $("input[data-height='cm']").blur(function(){ var text = $(this).val(); if($(this).val().length > 0) { if(!$(this).val().contains("cm")) { $(this).val(text + " cm"); }
$("input[data-height='cm']").blur(function(){
var text = $(this).val();
if($(this).val().length > 0) {
if(!$(this).val().contains("cm")) {
$(this).val(text + " cm");
}
}
});
在chrome上给出错误
未捕获的TypeError:$(…).val(…).contains不是函数
我怎样修理它请帮忙
谢谢。使用
indexOf
而不是contains
if($(this).val().indexOf("cm") == -1) {
$(this).val(text + " cm");
}
您可以使用
测试:
if(!/cm/.test($(this).val())) {
$(this).val(text + " cm");
}
Chrome中没有包含的方法。相反,您最好使用索引of
:
// Instead of...
if ($(this).val().contains('cm')) { /* val() contains 'cm' */ }
// ...do this:
if ($(this).val().indexOf != -1) { /* val() contains 'cm' */ }
请注意,indexOf
如果在字符串中未找到occurence,则返回-1;如果找到occurence,则返回>=0。.contains()
检查DOM元素是否是另一个DOM元素的后代,因此应将其用于jquery对象:
在这里,您可以使用indexOf()
函数检查value属性的内容
if($(this).val().indexOf("cm")>=0) {
}
您可以参考更多信息。功能不是那样工作的。您可能想使用indexOf
、match
或test
之类的方法。您试图在JavaScript字符串上使用jQuery contains
方法。contains方法是jQuery插件的,或者您可以在jQuery中使用dom元素选择器调用,但不能在字符串上调用此方法。为此,您必须使用indexOf.why cachetext
,如果您使用$(this).val()
?你应该更换它们(除了你设置的那一个)好吧,我理解,但它在firefox上是如何工作的?谢谢你节省了我很多时间