我使用下面的javascript和html创建一个按钮,从(单个)标签返回随机帖子。我的博客是:https://funatonce.blogspot.com
我有两个问题:
如何将此脚本与搜索词列表一起使用。我将输入一组搜索字词,每次点击都会产生不同的结果
我想为不同的标签添加多个具有随机功能的按钮,例如。如果我有15个按钮,我必须在页面上添加此脚本15次吗?我试过提供一个.js文件参考,但它没有用
http://yourjavascript.com/8012111731/random.js';类型= '文本/ JavaScript的'/>
<script type='text/javascript'>
//<![CDATA[
function showLucky(root){
var feed = root.feed;
var entries = feed.entry || [];
var entry = feed.entry[0];
for (var j = 0; j < entry.link.length; ++j) {
if (entry.link[j].rel == "alternate") {
window.location = entry.link[j].href;
}
}
}
function fetchLuck(luck){
script = document.createElement('script');
script.src = '/feeds/posts/summary/-/cool%20items?start-index='+luck+'&max-results=1&alt=json-in-script&callback=showLucky';
script.type = 'text/javascript';
document.getElementsByTagName('head')[0].appendChild(script);
}
function readLucky(root){
var feed = root.feed;
var total = parseInt(feed.openSearch$totalResults.$t,10);
var luckyNumber = Math.floor(Math.random()*total);
luckyNumber++;
fetchLuck(luckyNumber);
}
function beautyandpersonalcare(){
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = '/feeds/posts/summary/-/cool%20items?max-results=0&alt=json-in-script&callback=readLucky';
document.getElementsByTagName('head')[0].appendChild(script);
}
//]]>
</script>
<div class="randombtncontainer">
<a href="#" onclick="beautyandpersonalcare()" class="randombtn btn-1">Show Random Products</a>
</div>
答案 0 :(得分:0)
您可以在博客上使用jQuery ajax和本机搜索功能而不是JSON提要来获取搜索结果
<div id="container"></div>
<script>
//<![CDATA[
var counter = 0;
function getList(terms) {
$("#container").load("/search?q="+terms[counter].replace(' ', '+')+" #Blog1 .post");
counter++;
}
//]]>
</script>
<button onclick="getList(['mobile cases','mobile accessories','mobile apps'])">Show Random List</button>
要添加具有不同标签的多个按钮,您无需像在js文件中那样重复您的功能,在beautyandpersonalcare()
中将标签作为参数传递,并根据需要添加按钮。
<script type='text/javascript'>
//<![CDATA[
var label = "";
function showLucky(root){
var feed = root.feed;
var entries = feed.entry || [];
var entry = feed.entry[0];
for (var j = 0; j < entry.link.length; ++j) {
if (entry.link[j].rel == "alternate") {
window.location = entry.link[j].href;
}
}
}
function fetchLuck(luck){
script = document.createElement('script');
script.src = '/feeds/posts/summary/-/'+label+'?start-index='+luck+'&max-results=1&alt=json-in-script&callback=showLucky';
script.type = 'text/javascript';
document.getElementsByTagName('head')[0].appendChild(script);
}
function readLucky(root){
var feed = root.feed;
var total = parseInt(feed.openSearch$totalResults.$t,10);
var luckyNumber = Math.floor(Math.random()*total);
luckyNumber++;
fetchLuck(luckyNumber);
}
function beautyandpersonalcare(lab){
label = lab;
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = '/feeds/posts/summary/-/'+label+'?max-results=0&alt=json-in-script&callback=readLucky';
document.getElementsByTagName('head')[0].appendChild(script);
}
//]]>
</script>
<div class="randombtncontainer">
<a href="#" onclick="beautyandpersonalcare('labelName1')" class="randombtn btn-1">Show Random Products</a>
<a href="#" onclick="beautyandpersonalcare('labelName2')" class="randombtn btn-2">Show Random Products</a>
<a href="#" onclick="beautyandpersonalcare('labelName3')" class="randombtn btn-3">Show Random Products</a>
...
</div>