2017-04-12 52 views
0

如果我想显示install_option div 只有如果单词“Apple iPad”出现在标题中?jQuery - 如果它只匹配两个特定单词,则显示div

也可以做匹配,如果一个产品叫iPad的(小写)或iPad

<h1 id="pbtitle">Apple iPad 3</h1> 


<div class="install_option" style="display:none;">    
    <div style="width:35%; box-sizing:border-box; float:left;"> 
     <h3 style="font-weight:bold;">Would you like to view accessories?</h3> 
    </div> 

    <div class="apple_install_option" style="padding-top:20px;"> 
     <a href="https://www.apple.com" target="_blank"> 
       <img alt="Apple.com" src="http://cdn.cultofmac.com/wp-content/uploads/2012/10/lightningconnector.jpg" style="margin-right:20px; max-width:200px; height: auto;"> 
     </a> 
    </div> 

</div> 


$(".install_option").each(function() { 
    var brand = $("h1#pbtitle").text().toLowerCase(); 
    if (brand != "Lenovo") { 
    $('.install_option').css('display', 'block'); 
    } 
}); 

https://jsfiddle.net/ote8w56v/

+0

上面显示的的jsfiddle不起作用。这个可以:https://jsfiddle.net/54eg8cn6/ – Syno

+0

你可以使用'.indexOf',就像[this](https://jsfiddle.net/ote8w56v/3/) – George

回答

0

您可以使用indexOf,看是否字符串包含了一些话,如果它会返回索引,如果不是,则返回-1,因此只需检查它是否不返回-1,如下所示:

$(".install_option").each(function() { 
 
    var brand = $("h1#pbtitle").text().toLowerCase(); 
 
    if (brand.indexOf("ipad") != -1) { 
 
    $('.install_option').css('display', 'block'); 
 
    } 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<!-- Product names include Apple iPad, Acer Iconia, Lenovo Thinkpad --> 
 

 
<h1 id="pbtitle">Apple Ipad 3</h1> 
 

 

 
<div class="install_option" style="display:none;">    
 
    <div style="width:35%; box-sizing:border-box; float:left;"> 
 
    \t <h3 style="font-weight:bold;">Would you like to view accessories?</h3> 
 
    </div> 
 

 
    <div class="apple_install_option" style="padding-top:20px;"> 
 
\t \t <a href="https://www.apple.com" target="_blank"> 
 
\t \t \t \t <img alt="Apple.com" src="http://cdn.cultofmac.com/wp-content/uploads/2012/10/lightningconnector.jpg" style="margin-right:20px; max-width:200px; height: auto;"> 
 
\t \t </a> 
 
\t </div> 
 

 
</div>

+0

它怎么能在一个范围内触发联想,微软,宏基等关键字? – me9867

0

这样的:

$(".install_option").each(function() { 
    var thisElement=$(this); 
    var brand = thisElement.find("h1").text().toLowerCase(); 
    if (/ipad/.test(brand)) { 
     thisElement.css('display', 'block'); 
    } 
}); 
0

使用indexOf你可以做到这一点。欲了解更多详情,请参阅Link

$(".install_option").each(function() { 
 
    var brand = $("h1#pbtitle").text().toLowerCase(); 
 
    if (brand.indexOf("ipad") != -1) { 
 
    $('.install_option').css('display', 'block'); 
 
    } 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<!-- Product names include Apple iPad, Acer Iconia, Lenovo Thinkpad --> 
 

 
<h1 id="pbtitle">Apple Ipad 3</h1> 
 

 

 
<div class="install_option" style="display:none;">    
 
    <div style="width:35%; box-sizing:border-box; float:left;"> 
 
    \t <h3 style="font-weight:bold;">Would you like to view accessories?</h3> 
 
    </div> 
 

 
    <div class="apple_install_option" style="padding-top:20px;"> 
 
\t \t <a href="https://www.apple.com" target="_blank"> 
 
\t \t \t \t <img alt="Apple.com" src="http://cdn.cultofmac.com/wp-content/uploads/2012/10/lightningconnector.jpg" style="margin-right:20px; max-width:200px; height: auto;"> 
 
\t \t </a> 
 
\t </div> 
 

 
</div>

+0

它怎么能触发一系列的关键字,如联想,微软,宏碁等? – me9867

相关问题