我尝试遍历一系列div并检索数据属性以与Gigya社交共享API一起使用。 Gigya API文档列于此处:https://developers.gigya.com/display/GD/postBookmark+Working+Example
我已经使用jQuery .each成功循环了我的div并将结果记录在控制台中,但是我无法将数据属性的值转换为Gigya代码。现在它获取最后一个div项的数据属性值。代码如下。谢谢。
HTML:
<div class="grid-item" data-title="The First Post Title">
<h2>The First Post Title</h2>
</div>
<div class="grid-item" data-title="The Second Post Title">
<h2>The Second Post Title</h2>
</div>
<div class="grid-item" data-title="The Last Post Title">
<h2>The Last Post Title</h2>
</div>
jQuery的:
$(".grid-item").each(function(){
$this = $(this);
var dataTitle = $this.data('title');
// console log each title
console.log(dataTitle);
// GIGYA CODE
// Define an image media item:
image = {
type: 'image',
src: 'https://demo.gigya.com/images/recipe3.png',
href: 'https://demo.gigya.com/recipe3.php'
};
// Define a UserAction object specific for Twitter
uatw = new gigya.socialize.UserAction();
uatw.setLinkBack("https://demo.gigya.com/recipe3.php");
// attempt to set the title for Gigya. Currently outputting the last div's title :(
uatw.setTitle(dataTitle);
// Variable definition for Twitter share, note the Twitter specific UA object
TWParams = {
provider: 'twitter',
url: 'https://demo.gigya.com/recipe3.php',
userAction: uatw,
cid: 'Twitter Shares from Gigya postBookmark demo.'
};
});