JavaScript DOM 导航
我正在编写一段代码,允许用户通过页面上的 HTML 元素进行搜索。用户选择他们想要搜索的元素,然后代码遍历每个元素,逐个突出显示它们。如果用户从开始到结束搜索一个元素。但是,如果在一个元素搜索的中间他们将选择切换到另一个元素,则程序可能会因不同原因而开始失败,但主要是因为计数超出范围,例如说我在elementArray[4](其中计数 = 4)对于一个输入元素..然后用户切换到表元素(数组中可能总共只有 2 个元素)那么索引将超出范围..
有人可以看一下代码并也许建议更好的方法实施方式?我认为我应该有一个单独的函数来处理当用户在另一个元素搜索中间切换元素选择时的情况?
谢谢
[{
type: 'select',
id: 'findNext',
label: 'Find next :',
isChanged: false,
labelLayout: 'horizontal',
accessKey: 'R',
items: [
['Form', 'form'],
['Input', 'input'], // Checkbox, radio, textfield, button, image
['Table', 'table'],
['Textarea', 'textarea'],
['Select', 'select'],
['Anchor', 'a'], // [option, ]
['Image', 'img']
],
}, {
type: 'button',
align: 'left',
style: 'width:100%',
label: 'Find Next',
id: 'findX',
onClick: function () {
var dialog = this.getDialog();
if (typeof currentElement != 'undefined') {
x = currentElement;
x.removeAttribute('style'); // Remove the highlight from the current element
}
if (count != null) {
count++;
} else {
count = 0;
}
if (selectOptionArray == null) {
selectOptionArray = new Array();
}
var selectOption = dialog.getValueOf('find', 'findNext'); // Get user selection e.g input, table etc
elementArray = documentNode.getElementsByTagName(selectOption);
currentElement = elementArray[count]; // Keep a reference of the current choice so the higlight can be removed on line 7
getSelection();
function getSelection() {
if (count > 0) { // Check if there are two elements to compare
areElementsSame();
}
if ((elementArray.length > 0) && (count < elementArray.length)) { // Is count out of bounds?
nextElement(count, elementArray);
} else {
alert("Search complete..No elements exist.");
count = null;
}
}
function areElementsSame() {
if (count >= elementArray.length) {
count = 0;
nextElement(count, elementArray);
} else if (elementArray[count].nodeName == x.nodeName) {
nextElement(count, elementArray);
} else {
count = 0;
nextElement(count, elementArray);
}
}
function nextElement() {
if (count < elementArray.length) {
elementArray[count].setAttribute('style', 'background-color: blue');
elementArray[count].scrollIntoView(true);
} else {
alert('count is > elementArray.length, all elements navigated');
}
}
}
Im writing a piece of code which allows the user to search by HTML element on a page..The user selects what element they want to search for, and then the code traverses each element highlighting them one by one..The code works fine if the user searches for one element from start to finish..If however, in the middle of one element search they switch their choice to another element the program can start failing for different reasons but primarily because the count goes out of bound e.g say im on elementArray[4] (where count = 4) for one input element.. then the user switches to table element (which may only have a total of 2 elements in the array) then the index will be out of bounds..
Can someone have a look at the code and perhaps suggest a better way of implementing? I think i should have a seperate function to deal with the scenario when a user switches the element choice in the middle of another element search??
Thanks
[{
type: 'select',
id: 'findNext',
label: 'Find next :',
isChanged: false,
labelLayout: 'horizontal',
accessKey: 'R',
items: [
['Form', 'form'],
['Input', 'input'], // Checkbox, radio, textfield, button, image
['Table', 'table'],
['Textarea', 'textarea'],
['Select', 'select'],
['Anchor', 'a'], // [option, ]
['Image', 'img']
],
}, {
type: 'button',
align: 'left',
style: 'width:100%',
label: 'Find Next',
id: 'findX',
onClick: function () {
var dialog = this.getDialog();
if (typeof currentElement != 'undefined') {
x = currentElement;
x.removeAttribute('style'); // Remove the highlight from the current element
}
if (count != null) {
count++;
} else {
count = 0;
}
if (selectOptionArray == null) {
selectOptionArray = new Array();
}
var selectOption = dialog.getValueOf('find', 'findNext'); // Get user selection e.g input, table etc
elementArray = documentNode.getElementsByTagName(selectOption);
currentElement = elementArray[count]; // Keep a reference of the current choice so the higlight can be removed on line 7
getSelection();
function getSelection() {
if (count > 0) { // Check if there are two elements to compare
areElementsSame();
}
if ((elementArray.length > 0) && (count < elementArray.length)) { // Is count out of bounds?
nextElement(count, elementArray);
} else {
alert("Search complete..No elements exist.");
count = null;
}
}
function areElementsSame() {
if (count >= elementArray.length) {
count = 0;
nextElement(count, elementArray);
} else if (elementArray[count].nodeName == x.nodeName) {
nextElement(count, elementArray);
} else {
count = 0;
nextElement(count, elementArray);
}
}
function nextElement() {
if (count < elementArray.length) {
elementArray[count].setAttribute('style', 'background-color: blue');
elementArray[count].scrollIntoView(true);
} else {
alert('count is > elementArray.length, all elements navigated');
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不知道这是否满足您的所有要求,但这是一个开始
编辑:这是一个演示
使用示例:
Don't know if this meets all your requirements, but it's a start
Edit: Here's a demo
Usage example: