Tôi đã tìm ra điều này. Đây là cách để thực hiện công việc này.
Ý tưởng chung là:
- Trong HTML của bạn thiết lập các
class
của <a>
của bạn cho các nút chia sẻ twitter là một cái gì đó khác hơn .twitter-share-button
. Đồng thời, hãy cung cấp <a>
một style="display:none;"
.
- Trong HTML của bạn, nơi bạn muốn nút chia sẻ twitter xuất hiện, hãy tạo
<div>
(hoặc <span>
) với số id
duy nhất.
- Trong jQuery của bạn, khi bạn muốn thiết lập các dữ liệu cho nút chia sẻ twitter bạn sẽ:
- 'clone()
the hidden, mis-named,
' thẻ từ bướC# 1
- Mở bản sao này, thiết lập, vv
data-url
.. các thuộc tính như bạn muốn
- Tháo thuộc tính
style
từ clone (thôi ẩn nó)
- Thay đổi
class
của clone để twitter-share-button
append()
bản sao này vào số <div>
của bạn từ bước 2.
- Sử dụng
$.getScript()
để tải và chạy Javascript của Twitter. Điều này sẽ chuyển đổi nhân bản của bạn <a>
thành một với tất cả các goop đúng.
Dưới đây là HTML của tôi (trong Jade):
a.twitter-share-button-template(style="display:none;", href='https://twitter.com/share=', data-lang='en',
data-url='http://urlthatwillbe_dynamically_replaced',
data-via='ckindel', data-text='Check this out!',
data-counturl='http://counturlthatwillbe_dynamically_replaced') Share with Twitter
#twitter-share-button-div
Sau đó, trong .js client-side của bạn:
// the twitter share button does not natively support being dynamically
// updated after the page loads. We want to give it a unique (social_id)
// link whenver this modal is shown. We engage in some jQuery fun here to
// clone a 'hidden' twitter share button and then force the Twitter
// Javascript to load.
// remove any previous clone
$('#twitter-share-button-div').empty()
// create a clone of the twitter share button template
var clone = $('.twitter-share-button-template').clone()
// fix up our clone
clone.removeAttr("style"); // unhide the clone
clone.attr("data-url", someDynamicUrl);
clone.attr("data-counturl", someDynamicCountUrl);
clone.attr("class", "twitter-share-button");
// copy cloned button into div that we can clear later
$('#twitter-share-button-div').append(clone);
// reload twitter scripts to force them to run, converting a to iframe
$.getScript("http://platform.twitter.com/widgets.js");
tôi có những manh mối chính cho giải pháp này từ đây: http://tumblr.christophercamps.com/post/4072517874/dynamic-tweet-button-text-using-jquery
Tôi đang cố gắng hoàn thành về cơ bản cùng một điều. Tôi đã thử cập nhật thuộc tính 'data-url' động với điều này:' $ ('. Twitter-share-button'). Attr ("data-url", "http: // mynewlink"); 'nhưng javascript twitter có đã chạy và điều này không hoạt động. Cứu giúp! – tig