如何在 JavaScript 中写入标签?

How to write hashtag in JavaScript?

我有这样的功能:

function tweet(){
  window.open("https://twitter.com/intent/tweet?text=My text.  #myHashtag"
);
}

但是 JavasScript 在 # 符号处停止。怎么写呢?

使用%23,这是#的编码形式。

如果这是来自用户,您确实应该在将其放入查询字符串之前使用 encodeURIComponent

根据 twitter 文档,您可以将主题标签作为 &hashtags=

传递
https://twitter.com/intent/tweet?text=My+text&hashtags=bransonpickel

https://dev.twitter.com/web/tweet-button/web-intent

对(输入)文本进行编码:

let input = "My text.  #myHashtag";
let baseUrl = "https://twitter.com/intent/tweet?text=";
// window.open(baseUrl + encodeURIComponent(input));
console.log(baseUrl + encodeURIComponent(input));