Javascript 未捕获类型错误:$(此)。搜索不是函数

Javascript 未捕获类型错误:$(此)。搜索不是函数,javascript,function,typeerror,Javascript,Function,Typeerror,未捕获类型错误:$(此)。搜索不是函数 $(document).ready(function(){ $('#caption').on('keypress', function () { var n = $(this).search('#'); if(n != "-1"){ window.alert("There's a hash"); }else{

未捕获类型错误:$(此)。搜索不是函数

$(document).ready(function(){
    $('#caption').on('keypress', function () { 
            var n = $(this).search('#');
            if(n != "-1"){
                window.alert("There's a hash");
            }else{
                 window.alert("There's not a hash");
            }


    });
});
如果要在输入中搜索字符,可以使用
#

记住,是用来正则表达式的。否则会更快

 $(document).ready(function(){
    $('#caption').on('keypress', function () { 
        var n = $(this).val();
        if(n.indexOf("#") > -1){
            window.alert("There's a hash");
        }else{
             window.alert("There's not a hash");
        }

   });
});
结果:

如果要在输入中搜索字符
#
,则可以使用

记住,是用来正则表达式的。否则会更快

 $(document).ready(function(){
    $('#caption').on('keypress', function () { 
        var n = $(this).val();
        if(n.indexOf("#") > -1){
            window.alert("There's a hash");
        }else{
             window.alert("There's not a hash");
        }

   });
});

结果:
搜索是字符串类型的JavaScript方法


因此,如果您想使用
search
,那么您的行
var n=$(this.search('#')
应更改为
var n=$(this.val().search('#')
var n=$(this.text().search('#')
取决于
#caption
元素的标记。

搜索
是字符串类型的JavaScript方法


因此,如果您想使用
search
,那么您的行
var n=$(this.search('#')
应更改为
var n=$(this.val().search('#')
var n=$(this.text().search('#')取决于
#caption
元素的标记。

什么是
#caption
,您以什么方式搜索
#
?它是值、HTML还是属性?#是TextArea的值何谓
#caption
,您以何种方式搜索
#
?它是值、HTML还是属性?#是textarea的值