Javascript:动态更改点击具有"window.open"



我想更改onclick的值,我尝试过,但没有成功。有什么帮助吗?

<a href='#' id='social' onClick="">click</a>
document.getElementById("social").onclick = "open_new_window('https://twitter.com/share?url=https%3A%2F%2F"+url+"&amp;via=iguestblogger&amp;text="+title+"');";

注:urltitleopen_new_window()已定义,工作正常。

不更改onclick属性的值,只需绑定事件即可。

document.getElementById("social").onclick = function() {
  open_new_window('https://twitter.com/share?url=https%3A%2F%2F'+url+'&amp;via=iguestblogger&amp;text='+title);
};
document.getElementById("social").onclick = function(){open_new_window('https://twitter.com/share?url=https%3A%2F%2F"+url+"&amp;via=iguestblogger&amp;text="+title+"');};

假设url和title以及函数open_new_window已经在前面定义过了,也要注意你的报价

<a href='#' id='social' >click</a>
<script>
document.getElementById("social").onclick = function()
  open_new_window("https://twitter.com/share?url=https%3A%2F%2F"+url+"&amp;via=iguestblogger&amp;text="+encodeURI(title));
  return false;
}
</script>
document.getElementById("social").onclick = open_new_window('https://twitter.com/share?url=https%3A%2F%2F"+url+"&amp;via=iguestblogger&amp;text="+title+"');

相关内容

  • 没有找到相关文章

最新更新