您好,我希望根据页面内容显示一个 div。它将出现在动态生成的商店页面上。我需要在购物车中的特定商品上显示通知 - 例如,如果某人的购物车中有一只“玩具 Frog ”,则需要显示“ Frog 促销.div”。
我一直在尝试改编以下脚本/html以供使用
感谢任何帮助。戴夫
<div class="contactUs"?>contact</div>
<div class="hideThis"?>xxx</div>
<script type="text/javascript">
if (jQuery("div.contactUs:contains('toy frog')").length) {
jQuery(".hideThis").css("display","none");
}
</script>
============================================= =============
到目前为止,感谢大家的帮助。我对 JS 一无所知!
我不认为我已经清楚地阅读/尝试了所有这些..
我的目标是
1**。当带有“订阅”一词的产品添加到我的购物车时,我需要显示一条 html 消息。**
我尝试了一些似乎在 jsfiddle 上有效的示例,但在我的服务器上却无效。这常见吗?
再次为大家干杯
最佳答案
我不明白您的代码示例(与您的问题描述相关:包含产品的购物车内容以显示促销 div),但我制作了一个 small code回答您的问题。
HTML
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body>
<!-- Into your page HTML, Imagine you have two parts -->
<!-- The first one is the cart that contain the item the user want to buy -->
<div class="cart">
*Cart*
<div class="sausages">sausages</div>
<!-- Try to remove comment of code following -->
<!-- <div class="fries">fries</div> -->
</div>
<br/>
<br/>
<!-- The second one is the content page that contain the div you want to display according to item into your cart -->
<div class="content">
*Content*
<div class="sausages" style="display:none">sausages</div>
<div class="fries" style="display:none">fries</div>
</div>
</body>
</html>
JS
var $content = $('.content');
var $cart = $('.cart');
// for each children element (el) into my div cart
$cart.children().each(function(index, el) {
// I try to find a element with my className into my div Content
// And if I find it Then I show it
$content.find('.' + el.className).show();
});
我已经更新了my code评论更清晰
关于javascript - 当页面包含单词时显示 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23494174/