href 不适用于锚点

href does not work on anchor

我有下面这一段javascript。它使用 Ajax 从 API 中获取引用。报价显示在报价 div 处,但 href 未在 twitterAnchor 上刷新。

    jQuery('#button').on('click', function(){
            
        jQuery.ajax({
        url: "http://quotesondesign.com/wp-json/posts?filter[orderby]=rand&  filter[posts_per_page]=1&_jsonp=mycallback",
        type: 'GET',
        dataType: "jsonp",
        success: function(json){
            document.getElementById('quote').innerHTML = json[0].content;
            document.getElementById('twitterAnchor').href =  "https://twitter.com/intent/tweet?text=" + json[0].content;    
            }
        });
    });
<!-- Here is the anchor. -->

      <div id="tweet">
          <a id ="twitterAnchor" href="" class="twitter-share-button" target="_blank" >Tweet</a>  
      </div>

当我点击 twitterAnchor 时,页面被刷新。控制台什么也没显示,有人知道发生了什么事吗?

在尝试找出错误一天后...这就是解决方案:

jQuery('#button').on('click', function(){

    jQuery.ajax({
    url: "http://quotesondesign.com/wp-json/posts?filter[orderby]=rand&filter[posts_per_page]=1&_jsonp=mycallback",
    type: 'GET',
    dataType: "jsonp",
    success: function(json){
        document.getElementById('quote').innerHTML = json[0].content; 
        }
    });
});

jQuery('#twitterAnchor').on('click', function () {
    jQuery(this).attr("href","https://twitter.com/intent/tweet?text=" + document.getElementById('quote').innerHTML);
});

问题是:如果您尝试更改 ajax 连接内的 href,它不起作用。我真的不知道为什么,我会很高兴知道。

不要为 href 赋新值,而是使用 jQuery attr 方法。

$('#twitterAnchor').attr('href', 'https://twitter.com/intent/tweet?text=' + json[0].content);