如何将文本传递到javascript变量/函数中?

如何将文本传递到javascript变量/函数中?,javascript,jquery,Javascript,Jquery,我想创建内联的“tweetablequotes”。例如: 1) 用户单击一行文本,并用突出显示/不同的颜色表示可单击。例如 <span class="tweetable_quote">this is an amazing quote</span> 我如何将文本(“这是一个惊人的引用”)作为URL编码变量传递到Twitter意图URL中?请注意,在同一页上可能有几个“可推文引用” 提前感谢您的帮助 [1] 更新 我尝试实现以下建议,在中添加以下代码: <script

我想创建内联的“tweetablequotes”。例如:

1) 用户单击一行文本,并用突出显示/不同的颜色表示可单击。例如

<span class="tweetable_quote">this is an amazing quote</span>
我如何将文本(“这是一个惊人的引用”)作为URL编码变量传递到Twitter意图URL中?请注意,在同一页上可能有几个“可推文引用”

提前感谢您的帮助

[1]

更新

我尝试实现以下建议,在中添加以下代码:

<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>

//这将是具有“tweetable_quote”类的所有元素的单击处理程序。
$(“.tweetable_quote”)。在('click',function()上{
//$(此项)引用已单击的元素。
var text=$(this.text();
窗口打开(“https://twitter.com/intent/tweet?text=“+组件(文本));
});
这是一些可引用的文本。
加载页面时,控制台中将显示以下错误:

Uncaught TypeError: Object #<Object> has no method 'on'
(anonymous function)
uncaughttypeerror:对象#没有方法“on”
(匿名函数)

可以使用类似的方法来完成


可以这样做,使用


也许这就是你想要的-

//  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-通过替换 某些字符的每个实例由一个、两个、三个或四个转义组成 表示字符UTF-8编码的序列(仅适用于 由两个“代理”组成的字符的四个转义序列 字符)


也许这就是你想要的-

//  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-通过替换 某些字符的每个实例由一个、两个、三个或四个转义组成 表示字符UTF-8编码的序列(仅适用于 由两个“代理”组成的字符的四个转义序列 字符)

试试这个:

<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>

$(函数(){
变量$quoteEl=$('.tweetable'u quote');
$quoteEl.on('click',function(){
var text=$(this.text();
窗口打开(“https://twitter.com/intent/tweet?text=“+组件(文本));
});
});
试试这个:

<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>

$(函数(){
变量$quoteEl=$('.tweetable'u quote');
$quoteEl.on('click',function(){
var text=$(this.text();
窗口打开(“https://twitter.com/intent/tweet?text=“+组件(文本));
});
});

我为这次旅行表示歉意和感谢,Lix!我会回去感谢那些曾经帮助过我的人。我为这次旅行表示歉意和感谢,Lix!我会回去感谢那些在过去帮助过我的人。谢谢,@Lix,但是当页面加载时,我会收到以下控制台错误:Uncaught TypeError:Object#没有方法“on”测试:(匿名函数)代码:“tweetable_quote”类$(“.tweetable_quote”).on('click',function(){var text=$(this).text();window.open(“);});这是一个可引用的句子。谢谢,@Lix,但当页面加载时,我收到以下控制台错误:Uncaught TypeError:Object#没有“on”方法测试:(匿名函数)代码:“tweetable_quote”类。$(“.tweetable#”)on('click',function(){var text=$(this).text();window.open(“);});这是一个可引用的句子。基本上需要将其包装到文档准备函数中谢谢!工作非常漂亮:)基本上需要将其包装到文档准备函数中谢谢!作品优美:)