Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/406.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如果div中的文本是x,则只对x元素执行jquery_Javascript_Jquery_Html - Fatal编程技术网

Javascript 如果div中的文本是x,则只对x元素执行jquery

Javascript 如果div中的文本是x,则只对x元素执行jquery,javascript,jquery,html,Javascript,Jquery,Html,我想在span中比较这个文本“Free!”时执行一些jquery代码 这是我用过的代码,但不起作用 <script> if (jQuery('span.amount:contains("Free!")')) { jQuery('.single_add_to_cart_button').text('More Info'); jQuery(".amount").css("display", &qu

我想在span中比较这个文本“Free!”时执行一些jquery代码

这是我用过的代码,但不起作用

<script>
    if (jQuery('span.amount:contains("Free!")')) {
    jQuery('.single_add_to_cart_button').text('More Info');
        jQuery(".amount").css("display", "none");
    }</script>
用“数量”类更改所有跨度,而不仅仅是“内容”免费!"

因此,存在一种方法来隐藏和替换仅包含“Free!”!“在同一页中?

用于获取html值,然后与文本比较
免费!

这段代码应该可以工作

if (jQuery('div.amount').html() == 'Free!') {
    jQuery('.single_add_to_cart_button').text('More Info');
    jQuery(".amount").css("display", "none");
}
您还可以通过以下方式检查每个金额:

jQuery('div.amount').each(function(){
    if ($(this).html() == 'Free!') {
        jQuery('.single_add_to_cart_button').text('More Info');
        $(this).css("display", "none");
    }
});
jQuery('div.amount:contains(“Free!”))
返回jQuery对象。在条件语句中,jQuery对象的计算结果始终为true,即使它是空的

要知道元素是否存在,应检查长度:

if (jQuery('div.amount:contains("Free!")').length) { // 0 == false, else true
    jQuery('.single_add_to_cart_button').text('More Info');
    jQuery(".amount").css("display", "none");
}

还注意到,你的HTML是一个“代码>跨度/<代码>类“金额”,而不是<代码> div 。

<span class="amount">Free!</span>
<script> 
   var $free = jQuery('.amount:contains("Free")');
   if ($free.length) {
     jQuery('.single_add_to_cart_button').text('More Info');
     $free.hide();
   }
</script>
免费!
var$free=jQuery('.amount:contains(“free”));
如果($free.length){
jQuery('.single_add_to_cart_button')。text('More Info');
$free.hide();
}

一个对象始终是真正的Javascript这就是你需要的吗?:我发现了一个问题,请检查更新“更新确定问题是其他的”可以创建stacksnippets来演示吗?我有一个woocommerce网站,我想在价格免费时启动代码“查询('.single_add_to_cart_button')。text('More Info');jQuery(“.amount”).css(“显示”、“无”);问题是这个类的比较也在底部或在其他产品的旋转木马的侧边栏中,价格不同于“免费!,所以当开始这段代码时,用示例“45$”隐藏价格,我尝试了这一点,但不起作用“如果(jQuery('span.amount:contains('Free!”).length){}其他{jQuery('single_add_to_cart_按钮')。text('More Info');jQuery(.amount.css('display,'none');}“第一次从控制台收到此错误”未捕获的SyntaxError:输入意外结束(…),seconf也可以工作,当它为false时,这完全意味着您对我们隐藏了一些东西。您的问题中没有其他语句,第一个示例是在JSFIDLE中测试的。可能您的情况还不像您描述的那么简单。如何使用jquery intested$,因为使用$not work in my Site,您也可以执行var$=jquery.noConflict();但这是一个很长的故事。
if (jQuery('div.amount:contains("Free!")').length) { // 0 == false, else true
    jQuery('.single_add_to_cart_button').text('More Info');
    jQuery(".amount").css("display", "none");
}
<span class="amount">Free!</span>
<script> 
   var $free = jQuery('.amount:contains("Free")');
   if ($free.length) {
     jQuery('.single_add_to_cart_button').text('More Info');
     $free.hide();
   }
</script>