gpt4 book ai didi

javascript - 外部 Javascript 文件获取?

转载 作者:行者123 更新时间:2023-12-04 20:00:36 26 4
gpt4 key购买 nike

我目前正在使用这段代码:

var wordRandomizer = {
run: function (targetElem) {
var markup = this.createMarkup();
targetElem.appendChild(markup);
},
createMarkup: function () {
var that = this;
var frag = document.createDocumentFragment();
this.elem = document.createElement('span');
var button = document.createElement('button');
button.innerText = 'Change Item';
button.addEventListener('click', function () {
that.changeItem();
});
frag.appendChild(this.elem);
frag.appendChild(button);
return frag;
},
changeItem: function () {
var rand = this.getRandInt(1, this.items.length) - 1;
console.log(rand);
this.elem.innerText = this.items[rand];
},
getRandInt: function (min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
},
items: ['itemA', 'itemB', 'itemC', 'itemD']
};
wordRandomizer.run(document.body);

我的代码是一个按钮,按下它会捕获列表中的一项。但是,我不希望这些元素与生成器显示在同一页面上,因为人们只是查看源代码。我怎样才能做到这一点,一旦按下按钮,它就会从另一个位置抓取随机元素,人们无法使用源代码查看所有这些元素。

如果有帮助,您可以在此处查看代码 - http://jsbin.com/ESOdELU/1/edit

最佳答案

我将为您提供一个使用 PHP 的解决方案,因为它是一种免费的脚本语言,并且最有可能受到主机或默认 Web 服务器的支持...

对于初学者,这里是包含 jquery 和基本 AJAX 脚本的代码

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

<script type="text/JavaScript">
$(document).ready(function(){
$("#generate").click(function(){
$("#madlibs p").load("script.php");
});
});
</script>

这是script.php

的代码
<?php 
header("Cache-Control: no-cache");
// For testing you can use an inline array like the lines below
// Just remove the comment slashes "//" from the beginning of the line
// and comment out the external declarations

//$actors = array('Denzel Washington','Michael J. Fox','Jim Carey','Boris Kodjoe');
//$roles = array('Mental Patient','Homeless Musician','Drag Queen Detective','Tormented Mathematician');

// In production, you would put these in a text file or a database.
// For $actors, put an entry on each line of a text file and save it as 'leads.txt'
// Do the same with a separate file for $roles (roles.txt).
$actors = file("leads.txt");
$roles = file("roles.txt");

// This selects a random element of each array on the fly
echo $prefixes[rand(0,count($actors)-1)] . " stars as a "
. $suffixes[rand(0,count($roles)-1)] . " in the next blockbuster film.";
// Example output:
// Michael J. Fox stars as a Tormented Mathematician in the next blockbuster film.
?>

将它放在页面正文中,并确保为所有内容设置样式以供显示。

<body>
<div id="madlibs"><p> </p></div>
<button id="generate">Surprise Me!</button>
</body>

一些注意事项:- 您可以在 script.php 文件中包含基本布局 HTML,然后只需要显示结果的 DIV 的 ID $("#madlibs")

  • 您可以使用任何服务器端语言来获得相同的结果,只需将外部文件调用换成适当的名称和扩展名(.asp、.cfm 等)

这是帮助我完成类似元素的原始教程的链接: http://www.sitepoint.com/ajax-jquery/

希望对您有所帮助。抱歉,午餐时我想不出一个纯 Java 或 JavaScript 的解决方案。

关于javascript - 外部 Javascript 文件获取?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19663411/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com