【问题标题】:changing a link with javascript/jquery to an html element将带有 javascript/jquery 的链接更改为 html 元素
【发布时间】:2015-12-22 19:24:12
【问题描述】:

我正在尝试在我的网站上添加一个推特按钮,该按钮会在推特上发布一个报价,而通常推特链接是这样的:

      twitter.com/intent/tweet?text=what+is+happening? 

对于我来说,每次用户点击都会改变报价,我试图找出 div/span/h5/p 中的文本。

我尝试了以下方法:

      $("a[href='https://twitter.com/intent/tweet?text=hello+world']").attr("href", "https://twitter.com/intent/tweet?="+h5)

根本没有改变

如果这有什么不同的话,我是初学者......

【问题讨论】:

标签: javascript jquery html twitter


【解决方案1】:

如果我理解正确,这将有效。请注意,该链接将在一个新选项卡中打开,该选项卡似乎在 sn-p 中的 SO 上被阻止,但您可以看到它在工作 here on jsFiddle

$("img").click(function(){ 
  var arr = [
    'quote number 1',
    'quote number 2',
    'quote number 3',
    'quote number 4',
    'quote number 5',
    'quote number 6',
    'quote number 7',
    'quote number 8',
    'quote number 9',
    'quote number 10',
    'quote number 11',
    'quote number 12'
  ];
  
  var quote = arr[Math.floor(Math.random()*arr.length)]; // get random quote from the array
  $(".the-quote").html(quote); 
  var tweetUrl = 'https://twitter.com/intent/tweet?text='+quote.replace(/s+/g, '+');
  $("#sendTweet").attr('href', tweetUrl);
  
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Click image to change quote<br>
<img src="http://placehold.it/350x150"><br>
<div>
    <span>
        <h5>
            Current quote: <p class="the-quote">New Text to tweet</p>
        </h5>
    </span>
</div><br>
<a href="#" target="_blank" id="sendTweet">Click to Tweet</a>

【讨论】:

  • 实际上它只适用于第一个报价,但是在用户点击并且报价更改后,推文仍然是第一个报价
  • @nachal88 你能澄清一下吗?我不确定我明白你的意思
  • 我在网站上有一张图片,每次点击 h5 更改中的引用时,我使用以下代码:$("img").click(function(){ $(" .the-quote").html(arr[Math.floor(Math.random() * y)]) });所以当有人点击推特按钮时,我希望他发布当前报价
  • @nachal88 好的,检查我的编辑,这能解决问题吗?
  • 是的,效果很好,谢谢。我现在正在研究新代码以了解它...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-02
  • 2012-06-19
  • 1970-01-01
相关资源
最近更新 更多