如何附加 LinkedIn 分享标题?

How can I append LinkedIn share title?

我正在使用 jQuery 获取主标题部分,但我想在标题末尾添加一些自定义文本。我非常确信我用撇号弄乱了一些东西,但我不知道在哪里。

我要添加的文字是"CUSTOM SHARE TEXT"。

这就是我卡住的地方:

    <a href="#" onclick="window.open('http://www.linkedin.com/shareArticle?mini=true&url=' + jQuery('.active-review').attr('href') + '&title=' + jQuery('.active-review .head-container').text() + 'CUSTOM SHARE TEXT', '', '_blank, width=500, height=500, resizable=yes, scrollbars=yes'); return false;">
    <i class="fa fa-fw fa-linkedin"></i> LinkedIn</a>

感谢您的帮助。

我认为您的问题与 URL 中的空格和其他无效字符有关。

从文本构建 URL 时,您应该始终使用 encodeURIComponent

此外,正如您正确指出的那样,您忘记了一些撇号。

试试这个:

<a href="#" onclick="window.open('http://www.linkedin.com/shareArticle?mini=true&url=' + encodeURIComponent(jQuery('.active-review').attr('href')) + '&title=' + encodeURIComponent(jQuery('.active-review .head-container').text()) + encodeURIComponent('CUSTOM SHARE TEXT'), '', '_blank', 'width=500, height=500, resizable=yes, scrollbars=yes'); return false;">
    <i class="fa fa-fw fa-linkedin"></i> LinkedIn
</a>

问题出在 jQuery 组件中的过多空间。添加 trim() 功能后,问题似乎在我的案例中得到解决。附加到标题的文本总是在这里,但由于标题后有数十个%20而不可见。

这是适合我的最终代码。

    <a href="#" onclick="window.open('http://www.linkedin.com/shareArticle?mini=true&url=' + encodeURIComponent(document.URL) + '&title=' + encodeURIComponent(jQuery('.active-review .head-container').text().trim()) + encodeURIComponent(' CUSTOM TEXT'),'', '_blank, width=500, height=500, resizable=yes, scrollbars=yes'); return false;"
        ><i class="fa fa-fw fa-linkedin"></i> LinkedIn</a>