我想创建内联“tweetable引号”。例如:
1)用户点击一行文字,用高亮/不同的颜色标注,表示它是可点击的。 E.g。
<span class="tweetable_quote">this is an amazing quote</span>
2)使用包含引用的Twitter意图[1]打开窗口。 E.g。
window.open("https://twitter.com/intent/tweet?text=this%20is%20an%20amazing%20quote")
如何将文本(“这是一个惊人的引用”)作为在URL意图URL中使用的URL编码变量传递?请注意,同一页面上可能有几个“tweetable quotes”。
提前感谢您的帮助!
[1] https://dev.twitter.com/docs/intents
更新
我尝试实施以下建议,在以下代码中添加以下代码:
<script type="text/javascript">
// this will be the click handler for all elements with the "tweetable_quote" class.
$(".tweetable_quote").on('click',function(){
// $(this) refers to the element that was clicked.
var text = $(this).text();
window.open("https://twitter.com/intent/tweet?text="+encodeURIComponent(text));
});
</script>
<span class="tweetable_quote">This is some quotable text.</span>
加载页面时,控制台中会显示以下错误:
Uncaught TypeError: Object #<Object> has no method 'on'
(anonymous function)
答案 0 :(得分:1)
可以使用jQuery完成类似的操作。
$('.tweetable_quote').on('click', function() {
window.open("https://twitter.com/intent/tweet?text=" + encodeURIComponent ($(this).text()));
});
答案 1 :(得分:1)
也许这就是你要找的 -
// this will be the click handler for all elements with the "tweetable_quote" class.
$(".tweetable_quote").on('click',function(){
// $(this) refers to the element that was clicked.
var text = $(this).text();
window.open("https://twitter.com/intent/tweet?text="+encodeURIComponent(text));
});
我在这里使用encodeURIComponent()
功能正确编码文本 -
encodeURIComponent - 通过替换对统一资源标识符(URI)组件进行编码 每个特定字符的实例由一个,两个,三个或四个转义 代表字符的UTF-8编码的序列(仅限 由两个&#34;代理人组成的字符的四个转义序列&#34; 字符)。
答案 2 :(得分:1)
试试这个:
<script src="http://code.jquery.com/jquery-1.8.0.min.js"></script>
<script type="text/javascript">
$(function() {
var $quoteEl = $('.tweetable_quote');
$quoteEl.on('click',function(){
var text = $(this).text();
window.open("https://twitter.com/intent/tweet?text="+encodeURIComponent(text));
});
});
</script>