使用 JQuery 刷新和替换 div 中的内容
我会尝试让这变得快速而简单...我希望,
现在我有一个 div(在页面刷新时)可以更改内部内容。 正在更改的内容实际上位于同一个文档上,我不想从任何其他来源获取任何内容。
但我想做的是制作一个按钮来刷新该 div,以便用户可以循环浏览随机内容。它仍然可以随机循环,有没有办法做到这一点?这是我的代码任何帮助都会摇滚!谢谢你!
<div id="topjob-contain">
<ul class="jobcontrols">
<li><a href="#" id="jobchanger" >Load Another Job +</a></li>
</ul>
<div class="newjobz">
<h1><a href="#" target="_blank">#1 Random Job Title Here</a></h1>
<p>#1 Random Agency Name here</p>
<p>#1 Random Discription of job : .sed diam nonumm tincidunt ut laoreet dolorey nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. </p>
<br clear="all" />
</div>
<div class="newjobz">
<h1><a href="#" target="_blank">#2 Random Job Title Here</a></h1>
<p>#2 Random Agency Name here</p>
<p>#2 Random Discription of job : .sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. </p>
<br clear="all" />
</div>
<div class="newjobz">
<h1><a href="#" target="_blank">#4 Job Title Here</a></h1>
<p>#3 Random Agency Name here</p>
<p>#3 Random Discription of job : .sed diam nonummy t ut laoreet dolore magna aliquam erat volutpat. </p>
<br clear="all" />
</div>
<br clear="all" />
</div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<script>
////////////Randomize content on pageload
function randomFromTo(from, to){
return Math.floor(Math.random() * (to - from + 1) + from);
}
$(document).ready(function(){
var r = randomFromTo(1, $('div.newjobz').length);
$('div.newjobz').eq(r - 1).show();
});
</script>
Ill try and make this quick and easy...I hope,
Right now I have a div that ( on page refresh ) changes the content inside.
the content that is being changed is actully on the same document and im not trying to get any content form any other sources.
but what im trying to do is make a button that will refresh that div so that the user can cycle through the randomized content. it can still cycle through randomly, is there a way to do this? here is my code any help would Rock! thank you!
<div id="topjob-contain">
<ul class="jobcontrols">
<li><a href="#" id="jobchanger" >Load Another Job +</a></li>
</ul>
<div class="newjobz">
<h1><a href="#" target="_blank">#1 Random Job Title Here</a></h1>
<p>#1 Random Agency Name here</p>
<p>#1 Random Discription of job : .sed diam nonumm tincidunt ut laoreet dolorey nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. </p>
<br clear="all" />
</div>
<div class="newjobz">
<h1><a href="#" target="_blank">#2 Random Job Title Here</a></h1>
<p>#2 Random Agency Name here</p>
<p>#2 Random Discription of job : .sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. </p>
<br clear="all" />
</div>
<div class="newjobz">
<h1><a href="#" target="_blank">#4 Job Title Here</a></h1>
<p>#3 Random Agency Name here</p>
<p>#3 Random Discription of job : .sed diam nonummy t ut laoreet dolore magna aliquam erat volutpat. </p>
<br clear="all" />
</div>
<br clear="all" />
</div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<script>
////////////Randomize content on pageload
function randomFromTo(from, to){
return Math.floor(Math.random() * (to - from + 1) + from);
}
$(document).ready(function(){
var r = randomFromTo(1, $('div.newjobz').length);
$('div.newjobz').eq(r - 1).show();
});
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你就在那儿。需要进行一些更改。
首先添加一个 css 规则来隐藏你的 div。
然后像这样改变你的javascript:
jsFiddle
You were just about there. A few changes are needed.
First add a css rule to hide your divs.
Then change your javascript like so:
jsFiddle
使用 http://api.jquery.com/load/
Use http://api.jquery.com/load/
像这样的事情怎么样:
您可以通过删除变量声明并将其全部放在一长行上来缩短它,但我将其分解以使其更具可读性。
这将从带有
class
“newjobz”的随机div
中获取 HTML,并用id
覆盖div
的 HTML > 用它“divToChange”。How about something like this:
You could shorten this by getting rid of the variable declarations and doing it all on one long line, but I broke it up to make it more readable.
That will take the HTML from a random
div
withclass
"newjobz" and overwrite the HTML of thediv
withid
"divToChange" with it.