div 标签内的 AddThis 按钮未使用 Javascript 代码显示在 p 标签内
我正在尝试将分享按钮放入网站中。我发现了一个名为 AddThis 的好网站,以下是我能够将其组合在一起的内容:
<!-- AddThis Button BEGIN -->
<div class="addthis_toolbox addthis_default_style addthis_32x32_style"
addthis:url="http://example.com"
addthis:title="An Example Title"
addthis:description="An Example Description">
<a class="addthis_button_facebook"></a>
<a class="addthis_button_twitter"></a>
<a class="addthis_button_google_plusone_badge"></a>
<a class="addthis_button_linkedin"></a>
<a class="addthis_button_compact"></a>
<a class="addthis_counter addthis_bubble_style"></a>
</div>
<script type="text/javascript" src="http://s7.addthis.com/js/250/addthis_widget.js#pubid=xa-4f0a28da71ac4a61"></script>
<!-- AddThis Button END -->
我可以将所有内容粘贴到 html 页面中,并且它会正确显示。但是,链接、标题和描述需要是变量,而不是“http://example.com”。用户界面基于Javascript,并为用户提供连续评分的文章。链接、标题和描述变量为 story_link
、story_title
和 story_description
。
我尝试将所有这些内容放入 Javascript 中的变量中:
var story_link_href = '<a href="' + story_link + '" target="_blank" >Read more...</a><br /><br /><!-- AddThis Button BEGIN --><div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url="' + story_link + '" addthis:title="' + story_title + '" addthis:description="' + story_description + '"><a class="addthis_button_facebook"></a><a class="addthis_button_twitter"></a><a class="addthis_button_google_plusone_badge"></a><a class="addthis_button_linkedin"></a><a class="addthis_button_compact"></a></div><script type="text/javascript" src="http://s7.addthis.com/js/250/addthis_widget.js#pubid=xa-4f0a28da71ac4a61"></script><!-- AddThis Button END -->';
问题是,整个文章文本以及共享按钮都位于
标记内。据我所知,
标签内不起作用。
我对 Javascript 和 HTML 几乎一无所知,所以一切都只是猜测。我可以将第一组代码直接粘贴到 HTML 中并具有静态链接,或者如果将其粘贴到 Javascript 中则不显示任何内容。两者都没有按照应有的方式工作。
是否有一种方法可以将“story_link”、“story_title”和“story_description”变量传递到 HTML 中,并在新文章出现时不断传递新信息,或者是否有另一种方法将该代码集成到 Javascript 中,即
仍然围绕着一切吗?
I am trying to put share buttons into a website. I found a good site called AddThis, and here is what I was able to put together:
<!-- AddThis Button BEGIN -->
<div class="addthis_toolbox addthis_default_style addthis_32x32_style"
addthis:url="http://example.com"
addthis:title="An Example Title"
addthis:description="An Example Description">
<a class="addthis_button_facebook"></a>
<a class="addthis_button_twitter"></a>
<a class="addthis_button_google_plusone_badge"></a>
<a class="addthis_button_linkedin"></a>
<a class="addthis_button_compact"></a>
<a class="addthis_counter addthis_bubble_style"></a>
</div>
<script type="text/javascript" src="http://s7.addthis.com/js/250/addthis_widget.js#pubid=xa-4f0a28da71ac4a61"></script>
<!-- AddThis Button END -->
I can paste all of that into the html page, and it displays correctly. However, the link, title, and description need to be variables, not "http://example.com". The user interface is based on Javascript, and gives users articles to rate in succession. The link, title, and description variables are story_link
, story_title
, and story_description
.
I tried putting all of that into a variable in Javascript:
var story_link_href = '<a href="' + story_link + '" target="_blank" >Read more...</a><br /><br /><!-- AddThis Button BEGIN --><div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url="' + story_link + '" addthis:title="' + story_title + '" addthis:description="' + story_description + '"><a class="addthis_button_facebook"></a><a class="addthis_button_twitter"></a><a class="addthis_button_google_plusone_badge"></a><a class="addthis_button_linkedin"></a><a class="addthis_button_compact"></a></div><script type="text/javascript" src="http://s7.addthis.com/js/250/addthis_widget.js#pubid=xa-4f0a28da71ac4a61"></script><!-- AddThis Button END -->';
The problem with this is, the entire article text along with the share buttons are inside <p>
tags. As far as I can tell, <div>
tags don't work inside <p>
tags.
I know almost nothing about Javascript and HTML, so everything is just a guess. I can either paste the first bunch of code straight into HTML and have a static link, or not have anything show up if I paste it into Javascript. Neither works the way it should.
Is there is a way to pass the "story_link", "story_title", and "story_description" variables into HTML, and keep passing new information as new articles come up, or is there another way to integrate that code into Javascript, with having the <p>
still surrounding everything?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
内的
如果您想避免
中的
它不起作用,因为您可以'不要像您所做的那样编写脚本标记。您应该始终拆分脚本标记或将其附加到正文,如下所示:
这是更新。使用它代替您在问题中发布的代码。丢弃我在这个答案中发布的上述 JS 代码。我确信下面的代码会起作用。
<div>
s inside<p>
is not valid HTML, but they do work.If you want to avaoid
<div>
inside<p>
, replace "div" tags in addThis to "span" like below:It's not working because you can't write script tag the way you have done. You should always split the script tag or append it to the body as below:
Here is an update. Use this in place of the code you posted in your question. Discard above JS code I posted in this answer. I'm sure the below code will work.
该问题与我实现的一些代码有关,这些代码剥离了“类”属性,并且它正在从 AddThis 标签中剥离类。接下来,AddThis Javascript 文件未在正确的位置初始化,需要在适当的位置使用
addthis.init()
进行调用。 Sandeep 和我一起解决了这个问题,所以他得到了荣誉。The problem was related to some code I had implemented which stripped "class" attributes, and it was stripping class out of the AddThis tags. Next, the AddThis Javascript file wasn't initialized at the right place, and needed to be called with
addthis.init()
in the appropriate place. Sandeep worked with me to solve this issue, so he gets the credit.