簡體   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