我无法访问 index.jsp 中的嵌套 JavaScript 函数
Posted
技术标签:
【中文标题】我无法访问 index.jsp 中的嵌套 JavaScript 函数【英文标题】:I can't access nested JavaScript function in index.jsp 【发布时间】:2022-01-16 20:17:54 【问题描述】:我有一个应该在表格上执行分页的函数。在该函数中是另一个函数,当我单击导航栏中的数字时需要执行该函数。该代码在 VSCode 中运行良好。当我在eclipse中将代码放入index.jsp
时,函数给出错误:Uncaught ReferenceError: sort is not defined at htmlButtonElement.onclick
。
// get the table element
var table = document.getElementById("disposalTable"),
// number of rows per page
n = 5,
// number of rows of the table
rowCount = table.rows.length,
// get the first cell's tag name (in the first row)
firstRow = table.rows[0].firstElementChild.tagName,
// boolean var to check if table has a head row
hasHead = (firstRow === "TH"),
// an array to hold each row
tr = [],
// loop counters, to start count from rows[1] (2nd row) if the first row has a head tag
i,ii,j = (hasHead)?1:0,
// holds the first row if it has a (<TH>) & nothing if (<TD>)
th = (hasHead?table.rows[(0)].outerHTML:"");
// count the number of pages
var pageCount = Math.ceil(rowCount / n);
// if we had one page only, then we have nothing to do ..
if (pageCount > 1)
// assign each row outHTML (tag name & innerHTML) to the array
for (i = j,ii = 0; i < rowCount; i++, ii++)
tr[ii] = table.rows[i].outerHTML;
// create a div block to hold the buttons
table.insertAdjacentHTML("afterend","<div id='buttons'></div");
// the first sort, default page is the first one
sort(1);
// ($p) is the selected page number. it will be generated when a user clicks a button
function sort(p)
/* create ($rows) a variable to hold the group of rows
** to be displayed on the selected page,
** ($s) the start point .. the first row in each page, Do The Math
*/
var rows = th,s = ((n * p)-n);
for (i = s; i < (s+n) && i < tr.length; i++)
rows += tr[i];
// now the table has a processed group of rows ..
table.innerHTML = rows;
// create the pagination buttons
document.getElementById("buttons").innerHTML = pageButtons(pageCount,p);
// CSS Stuff
document.getElementById("id"+p).setAttribute("class","active");
// ($pCount) : number of pages,($cur) : current page, the selected one ..
function pageButtons(pCount,cur)
/* this variables will disable the "Prev" button on 1st page
and "next" button on the last one */
var prevDis = (cur == 1)?"disabled":"",
nextDis = (cur == pCount)?"disabled":"",
/* this ($buttons) will hold every single button needed
** it will creates each button and sets the onclick attribute
** to the "sort" function with a special ($p) number..
*/
buttons = "<input type='button' value='<< Prev' onclick='sort("+(cur - 1)+")' "+prevDis+">";
for (i=1; i<=pCount;i++)
buttons += "<input type='button' id='id"+i+"'value='"+i+"' onclick='sort("+i+")'>";
buttons += "<input type='button' value='Next >>' onclick='sort("+(cur + 1)+")' "+nextDis+">";
return buttons;
【问题讨论】:
运行页面时是否有效? 是的,它确实执行了,但是当我点击数字 2 按钮转到下一页时,它不想执行。 浏览器控制台中是否有错误消息? 是 - 错误:未捕获的 ReferenceError:排序未在 HTMLButtonElement.onclick 中定义 【参考方案1】:我想通了。而不是尝试访问嵌套函数。将寻呼机代码放入函数中。然后创建一个函数实例作为变量。并且每次都调用那个变量。
例如:
让 pager = new Pager();
那么onclick方法每次都会调用pager.sort(1)或者pager.sort(2)。
【讨论】:
以上是关于我无法访问 index.jsp 中的嵌套 JavaScript 函数的主要内容,如果未能解决你的问题,请参考以下文章
javaweb的index.jsp为啥是默认首页(我修改了index.jsp文件名字就会变为首页了)?
Myeclipse中tomcat启动,index.jsp访问出现404或者500错误!!
关于springmvc 只能在index.jsp页面显示图片的处理办法jsp页面无法显示图片