最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - does not contain using jquery - Stack Overflow

programmeradmin1浏览0评论

Below is the function that I am using in jquery

function addlnkfieldmkAndyr()
{
    var mymke =  $('h3:contains("Make")');
    var mymkeVal= $('h3:contains("Make")').closest("td").next();

    var myyr=  $('h3:contains("Year")');
    var myyrVal= $('h3:contains("Year")').closest("td").next();
}

The problem that is there is another field with the name as MakeandYear , so mymkeVal and myyrVal are getting the values from MakeandYear instead of just Make.

I would like to say

string.Contains("Make") && !string.Contains("MakeandYear). 

How do I do that in jquery , please help!

Below is the function that I am using in jquery

function addlnkfieldmkAndyr()
{
    var mymke =  $('h3:contains("Make")');
    var mymkeVal= $('h3:contains("Make")').closest("td").next();

    var myyr=  $('h3:contains("Year")');
    var myyrVal= $('h3:contains("Year")').closest("td").next();
}

The problem that is there is another field with the name as MakeandYear , so mymkeVal and myyrVal are getting the values from MakeandYear instead of just Make.

I would like to say

string.Contains("Make") && !string.Contains("MakeandYear). 

How do I do that in jquery , please help!

Share Improve this question edited Dec 2, 2011 at 15:36 Jakub 20.5k8 gold badges66 silver badges93 bronze badges asked Dec 2, 2011 at 15:34 JanetJanet 1,41114 gold badges41 silver badges63 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 6

Use .not , like $("div").not(":contains('Test')") ...

See: http://jqapi./#p=not

Andrey pointed to the .not function, which is a perfectly good answer. You can also use the :not selector, in bination with :contains (live example):

var mymke =  $('h3:contains("Make"):not(:contains("MakeAndYear"))');

The advantage of :not (the selector) over .not (the function) is that you don't add unnecessary elements to the jQuery object and then remove them. Does it matter? Almost certainly not, and be careful making any performance assumptions, although I think your use of :contains means throwing :not in won't do any harm. You'd have to have a truly enormous number of h3s for it to matter either way.

You'll want to end up with something like this. (I'm not sure what it would look like if you tried to do the whole thing inside the string.)

var mymke = $('h3:contains("Make")').not(':contains("MakeandYear")');
var mymkeVal = mymke.closest("td").next();

You can append :not(contains("and")) to your selectors. E.g.:

var mymke =  $('h3:contains("Make"):not(contains("and"))');
var mymkeVal= mymke.closest("td").next();
var myyr=  $('h3:contains("Year"):not(contains("and"))');
var myyrVal= myyr.closest("td").next();

But you should consider if there's a more exact way to identify the nodes you need than contains(). If you can control the output, you should mark the row with a class, or even include the Val in a data- attribute.

How about $('h3:contains("Make")').not(':contains("MakeAndYear")')

发布评论

评论列表(0)

  1. 暂无评论