JavaScript 上一个和下一个按钮循环浏览 .js 文件数组

发布于 2024-12-11 06:30:35 字数 1760 浏览 0 评论 0 原文

我在编写一些将循环访问 .js 文件数组的 JavaScript 时遇到困难。

我有一些 JavaScript 小部件保存在 .js 文件中。 我希望能够单击“下一个”或“上一个”按钮来循环浏览这些 .js 文件的数组,并调用小部件并将其显示在我的 HTML 页面上。如果这是更好的解决方案,它们可以显示在 iFrame 中。

我将继续研究,直到好心人帮忙。提前谢谢大家!

我尝试过:

<script>
function onWindowLoad(){
    document.getElementById('js_type').innerHTML = ****.settings.type;


var widget_arr = [1column.js,2column.js,1row.js,modal.js]; //etc..etc..
var currentWidget = 0;
theBtn.onRelease = function(){
currentWidget++;
if(currentWidget == widget_arr.length){
    currentWidget=0;
}
var selectedWidget = widget_arr[currentWidget];
//now you have a variable pointing to the next widget..
//what you do with it is up to you.. add the code you need..
}

以及

<SCRIPT LANGUAGE="JavaScript">
<!--
// Use the following variable to specify 
// the number of widgets
var NumberOfWidgets = 4

var widget = new Array(NumberOfWidgets)

// Use the following variables to specify the widget names:
widget[0] = "1column.js"
widget[1] = "2column.js"
widget[2] = "1row.js"
widget[3] = "modal.js"

var widgetNumber = 0

function NextWidget()
{
widgetNumber++
if (widgetNumber == NumberOfWidgets)
    widgetNumber = 0
document.widgets["VCRWidget"].src = widget[widgetNumber]
}  

function PreviousWidget()
{
widgetNumber--
if (widgetNumber < 0)
    widgetNumber = NumberOfWidgets - 1
document.widgets["VCRWidget"].src = widget[widgetNumber]

<IMG SRC="modal.js" NAME="VCRWidget">

}
//-->
</SCRIPT>

上一个和下一个按钮的代码:

<A HREF="javascript:PreviousWidget()">
<IMG SRC="prev.png" BORDER=0></A>
<A HREF="javascript:NextWidget()">
<IMG SRC="next.png" BORDER=0></A>

I am having difficulty writing some JavaScript that will cycle through an array of .js files.

I have some JavaScript widgets saved in .js files.
I want to be able to click a "Next" or "Previous" button to cycle through an array of those .js files and have the widgets called and displayed on my HTML page. They can be displayed in an iFrame if that would be a better solution.

I will continue researching until a kind soul helps out. Thanks a bunch in advance!

I have tried:

<script>
function onWindowLoad(){
    document.getElementById('js_type').innerHTML = ****.settings.type;


var widget_arr = [1column.js,2column.js,1row.js,modal.js]; //etc..etc..
var currentWidget = 0;
theBtn.onRelease = function(){
currentWidget++;
if(currentWidget == widget_arr.length){
    currentWidget=0;
}
var selectedWidget = widget_arr[currentWidget];
//now you have a variable pointing to the next widget..
//what you do with it is up to you.. add the code you need..
}

and this

<SCRIPT LANGUAGE="JavaScript">
<!--
// Use the following variable to specify 
// the number of widgets
var NumberOfWidgets = 4

var widget = new Array(NumberOfWidgets)

// Use the following variables to specify the widget names:
widget[0] = "1column.js"
widget[1] = "2column.js"
widget[2] = "1row.js"
widget[3] = "modal.js"

var widgetNumber = 0

function NextWidget()
{
widgetNumber++
if (widgetNumber == NumberOfWidgets)
    widgetNumber = 0
document.widgets["VCRWidget"].src = widget[widgetNumber]
}  

function PreviousWidget()
{
widgetNumber--
if (widgetNumber < 0)
    widgetNumber = NumberOfWidgets - 1
document.widgets["VCRWidget"].src = widget[widgetNumber]

<IMG SRC="modal.js" NAME="VCRWidget">

}
//-->
</SCRIPT>

Code for the previous and next buttons:

<A HREF="javascript:PreviousWidget()">
<IMG SRC="prev.png" BORDER=0></A>
<A HREF="javascript:NextWidget()">
<IMG SRC="next.png" BORDER=0></A>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

听不够的曲调 2024-12-18 06:30:35

一个想法是利用这样一个事实:即使是静态页面也可以在 url 中包含参数。例如,您可以:

  1. 创建将在
  2. 添加到此页面的 js 函数,给定小部件 js 文件名将创建一个
  3. ? 之后的字符串部分,从 document.location.href 中提取用于调用 (2) 中的函数的 js 文件的名称
  4. 通过查看main 中 页面动态创建 iframe,例如使用 contdiv.innerHTML = "";

通过这种方法,小部件将显示在单独的 html 页面中,而不会干扰您的主页。

One idea can be to use the fact thst even a static page can have parameters in the url. You can for example:

  1. create the html page that will be opened in the <iframe> with any js include you need (e.g. jquery)
  2. add to this page a js function that given a widget js filename will create a <script> tag loading the widget creation code.
  3. extract the name of the js file to use to call the function in (2) from document.location.href by looking at the part of the string after ?
  4. in your main page create dinamically the iframe using for example contdiv.innerHTML = "<iframe src=\"widgetpage.html?" + widgetname + ".js\"></iframe>";

With this approach the widgets will be shown in a separate html page without interferring with your main page.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文