2016-07-14 83 views
0

我想创建一个独立的共享按钮 - 我想通过视频链接填充共享窗口。我需要动态地做到这一点,因为链接变化 - 我可以通过在下面的HTML代码片段中定位“selected”类来做到这一点吗?它现在根据示例共享该网站,如何将网站位置更改为视频链接?从DOM元素动态填充共享按钮

按钮:

<a class="faceshare" href="http://www.facebook.com/sharer.php?u=https://example.com" target="_blank"> 

HTML:

<li class="feed-item selected"><div class="thumbnail-excerpt wprss-feed-thumbnail"> 
<a class="colorbox cboxElement" href="http://www.youtube.com/embed/Bj_N5o_-P_A"><img src="wp-content/uploads/cache/remote/i3-ytimg-com/3718811643.jpg" height="280" width="280"> 
</a>< 
</li> 

这是我迄今为止最接近的 - 它显示我的链接在控制台改变,但在DOM不会改变。

在控制台的结果是

"http://www.facebook.com/sharer.php?u=http://www.youtube.com/embed/Bj_N5o_-P_A" 

脚本

var link = jQuery('li.feed-item.selected a.colorbox.cboxElement').attr('href'); 
jQuery('a.faceshare').attr("href").replace('https://example.com', link) 

回答

3

您可以使用类似 $(".cboxElement").attr("href", "youLinkGoesHere")

UPDATE: 答案的完整版本可以在这里找到http://jsfiddle.net/y70zrm2s/3/

+0

不知道如果我关注。我需要动态获取href链接,然后替换https://example.com链接 –

+0

$(“.cboxElement”)。attr(“href”)。replace('example.com','yourLink') – tratto

+0

hm ,奇怪的是,上面没有工作,但接下来做$('。link')。each(function(){this.href = this.href.replace('example.com','yourLink');}); – tratto