繁体   English   中英

使用 data 属性获取值并显示在其他 div 上

[英]get value with data attribute and display on other div

我想使用 jquery 显示在此 div 上的按钮内找到的值“低于 1,000”

<div class="showvaluehere">here</div>

<button class="button" data-filter=".1000">1,000 below</button>

到目前为止我得到了这个但没有工作,它显示类“.1000”

var syo = $this.attr('data-filter');
$(this).parent().find('.showvaluehere').html(syo);

为清楚起见,我希望按钮的值在

以便它显示为这样;

<div class="showvaluehere">1,000 below</div>

谢谢

您正在获取数据过滤器属性的值,即 .1000

尝试这个

   $('.button').click(function() {
      var syo = $(this).text();
      $('.showvaluehere').html(syo);
    })

您的代码令人困惑,因为您引用了许多似乎与您显示的 HTML 和您描述的问题无关的元素和属性。

也就是说,您可以通过简单地将.showvaluehere元素的text()设置为与其相邻的button相匹配来执行所需的操作,如下所示:

 $('.showvaluehere').text(function() { return $(this).next('button').text(); });
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="showvaluehere">here</div> <button class="button" data-filter=".1000">1,000 below</button>

 $('.button').click(function() { $('.showvaluehere').text($(this).text()) })
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="showvaluehere">here</div> <button class="button" data-filter=".1000">1,000 below</button>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM