jQuery - 如果只匹配两个特定单词,则显示div

时间:2017-04-12 10:51:18

标签: javascript jquery css contains

如果我想显示install_option div ,如果单词" Apple iPad"是否出现在标题中?

如果产品名为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/

3 个答案:

答案 0 :(得分:0)

您可以使用indexOf来查看字符串是否包含某些单词,如果是,则会返回索引,否则会返回-1,所以请检查它是否不返回{ {1}},如下:

&#13;
&#13;
-1
&#13;
$(".install_option").each(function() {
  var brand = $("h1#pbtitle").text().toLowerCase();
  if (brand.indexOf("ipad") != -1) {
    $('.install_option').css('display', 'block');
  }
});
&#13;
&#13;
&#13;

答案 1 :(得分:0)

像这样:

<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;">
    	<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>

答案 2 :(得分:0)

使用indexOf您可以执行此操作。有关详细信息,请参阅Link

&#13;
&#13;
$(".install_option").each(function() {
  var brand = $("h1#pbtitle").text().toLowerCase();
  if (brand.indexOf("ipad") != -1) {
    $('.install_option').css('display', 'block');
  }
});
&#13;
<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;">
    	<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>
&#13;
&#13;
&#13;