如何创建JavaScript徽章或小部件? [英] How can I create a javascript badge or widget?
问题描述
我想创建一个显示链接列表的JavaScript徽章.我们将javascript托管在我们的域中.其他网站可以在其页面上放置一个空的div标签,并在底部添加对我们javascript的引用,该引用将呈现div标签中的内容.你如何实现这样的东西?
I want to create a javascript badge that displays a list of links. We host the javascript on our domain. Other sites can put an empty div tag on their page and at the bottom a reference to our javascript that would render the content in the div tag. How do you implement something like this?
推荐答案
我将给SCRIPT标签一个ID,并用DIV +内容替换脚本标签本身,这样一来,它们只需要包含一行代码即可.遵循以下原则:
I would give the SCRIPT tag an ID and replace the script tag itself with the DIV + contents, making it so they only have to include one line of code. Something along the lines of the following:
<script id="my-script" src="http://example.com/my-script.js"></script>
在脚本中,您可以一口气将SCRIPT标记换成DIV,如下所示:
In your script, you can swap out the SCRIPT tag for your DIV in one fell swoop, like so:
var scriptTag, myDiv;
scriptTag = document.getElementById('my-script');
myDiv = document.createElement('DIV');
myDiv.innerHTML = '<p>Wow, cool!</p>';
scriptTag.parentNode.replaceChild(myDiv, scriptTag);
这篇关于如何创建JavaScript徽章或小部件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!