通过jQuery动态Facebook共享按钮

时间:2014-12-20 03:28:17

标签: javascript jquery facebook facebook-javascript-sdk

HTML:

<div class="views-row views-row-2 views-row-even">
  <div class="sonuc">        
    <span>lorem ipsum dolor sit amet</span>  
  </div>  
  <div class="views-field views-field-nothing">        
    <span class="field-content"><a href="#" class="share">Share</a></span>  
  </div>  <div class="sonucbg"></div>
</div>
<div class="views-row views-row-2 views-row-even">
  <div class="sonuc">        
    <span>lorem ipsum dolor sit amet 2222</span>  
  </div>  
  <div class="views-field views-field-nothing">        
    <span class="field-content"><a href="#" class="share">Share</a></span>  
  </div>  <div class="sonucbg"></div>
</div>

JS:

$('#share').click(function() {

  var product_name = jQuery(".sonuc span").html();

  FB.ui({
    method: 'feed',
    name: product_name,
  }, function(response) {
    if(response && response.post_id){}
    else{}
  });
});

当我点击我的.share按钮时,弹出即可。但我想,例如单击第一个共享按钮,获取该文本。

我该怎么做?

1 个答案:

答案 0 :(得分:0)

您需要根据点击的按钮在.sonuc span实例中隔离views-row的实例

试试这个:

$('.share').click(function() {    
  var product_name = $(this).closest('.views-row').find(".sonuc span").html();
  /* FB code */
});

您目前拥有的内容将始终从页面中包含的第一个.sonic获取HTML