jquery val() contains()

2019-04-03 18:29发布

问题:

I want to know if the textarea value contains a certain word. This is not working for me.

var value = $('#embedModal textarea').val();
if($(value).contains('iframe')){...

回答1:

Try javascript

if (value.indexOf('iframe') >= 0) {

JQuery contains is for DOM elements, not strings.



回答2:

Try doing it like this:

$('#embedModal textarea:contains("iframe")').each(function() {
  //Do something
});

edit

Example



回答3:

this works:

$.contains( document.documentElement, document.body ); // true

var babyEl = $('div#id');
var daddyEl = $('div#ID');

if($.contains(babyEl, daddyEl)) {
    //do fun stuff
}


回答4:

Try following code.....

code:

             <textarea rows="4" cols="9">Something iframe</textarea>
             <textarea rows="4" cols="9">Something</textarea>

               var obj=$("textarea:contains('iframe')");

                   if(obj){

                            obj.css("background-color", "red");

                         }


回答5:

You can do this as well:

if($('#embedModal textarea:contains("iframe")').length > 0){
    //TODO: do stuff
}