Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何通过javascript更改链接HREF_Javascript - Fatal编程技术网

如何通过javascript更改链接HREF

如何通过javascript更改链接HREF,javascript,Javascript,我有一个表单,用户在其中输入URL。对该输入稍加调整后得到的值是立即向用户显示的变量#url 我需要让我的站点中的like和tweet按钮指向该URL。使他们的HREF等于变量#url 以下是共享按钮: <a id="twtbutton" href="http://twitter.com/share" class="twitter-share-button" data-count="horizontal" data-via="theclocktwt">Tweet</a>&

我有一个表单,用户在其中输入URL。对该输入稍加调整后得到的值是立即向用户显示的变量#url

我需要让我的站点中的like和tweet按钮指向该URL。使他们的HREF等于变量#url

以下是共享按钮:

<a id="twtbutton" href="http://twitter.com/share" class="twitter-share-button" data-count="horizontal" data-via="theclocktwt">Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script></div>

<fb:like id="fbbutton" send="false" layout="button_count" width="450" show_faces="false" font="" style="position: absolute; left:110px;"></fb:like>
</div>

在这里您可以看到它是如何工作的:


显示为“改进链接”的是我希望共享按钮使用的url。

请参考jquery网站上的以下页面:

应该能够使用attr('href','value)

试试这个:

$("#url").keypress(
    function(e) {
        if(e.keyCode == 13) {
            $("#output").attr('href', cleanURL($("#url").val()));
        }
    }
);

这就是你想要做的-

var yourElement = document.getElementById('yourHref');
 yourElement.setAttribute('href', '#url');

这会将id=twtbutton和id=fbbutton的html元素的“href”属性更改为存储在#url中的值,其中存储在#url中的值是字符串数据类型。这个答案利用了jQuery库。

您的问题缺少很多细节。按键是关于什么的?你想完成什么?见鬼,什么是
cleanURL()
?-1/2(四舍五入为0)。他显然有jQuery,所以使用jQuery。他有jQuery有关系吗?答案是有效的,问题并没有说“使用jquery”。我们尝试了这个方法,看起来很有效,但只有在调试器(Firefox)中。无论我在哪里放置或调用脚本,更新都不会反映在浏览器中。因此,在调试器中,我看到更新的链接,但显示的链接仍然是旧链接。IE中的行为相同,而且都是最新的。谢谢,但我不知道你为什么不改为“document.getElementById('yourref').setAttribute('href','#url');”我想,代码的易读性更好。@Liso22代码看起来像
$('a').attr(“href',”http://example.com");
显然example.com是您想要替换旧URL的新URL。我刚刚在JSFIDLE中尝试了它,它以某种方式修改了超链接字符串,而不是它指向的URL。您能对此答案提供一些解释吗?:DSure,我偶然发现了这个线程,它正在寻找一个示例,说明如何将给定html元素的属性“href”的值更改为新值。当前排名靠前的答案对我来说不太合适,但当我将代码(当然是使用jQuery)调整到上面所示的位置时,它对我来说很合适。所以我想提供这个片段,以防其他人和我一样对其他排名答案有疑问。
    $('#twtbutton').prop('href', #url);
    $('#fbbutton').prop('href', #url);