在javascript点击后获取数组的下一个元素,并以搜索形式显示
Getting next element of an array after javascript click and display it in search form
我想创建JS代码,在一些操作后(例如单击后)将数组中的下一个元素放入表单中。
我想在访问某些网站时在控制台(firebug,firefoxF12)中运行此代码。我创建了应该在stackoverflow上工作的代码的测试示例,但它没有,为什么
示例-stackoverflow.com:
在标题区域(id#问题标题)中单击后,数组中的下一个元素应显示在搜索表单输入中(名称q 当我在firefox控制台中运行代码时,它不起作用。 我该怎么修? var arr = ['foo', 'bar', 'baz'];
var i = 0;
function nextItem() {
i = i + 1; // increase i by one
i = i % arr.length; // if we've gone too high, start from `0` again
return arr[i]; // give us back the item of where we are now
}
window.addEventListener('load', function () {
document.getElementsByName('q').value = arr[0]; // initial value
document.getElementById('question-header').addEventListener(
'click', // we want to listen for a click
function (e) { // the e here is the event itself
document.getElementsByName('q').value = nextItem();
}
);
});
input
元素是自关闭标记,因此textContent
不起作用。它的值可以通过value
属性访问:
document.getElementById('search').value = nextItem();
更新:
根据问题更新,我认为代码中的主要问题是在页面加载时附加了侦听器。当您将代码粘贴到浏览器控制台时,您不需要这样做。这是SO:的工作代码
var arr = ['foo', 'bar', 'baz'];
var i = 0;
function nextItem() {
i = i + 1;
i = i % arr.length;
return arr[i];
}
// getElementsByName returns array of elements so access it by index
document.getElementsByName('q')[0].value = arr[0];
// because header contains inner link, add listener on it and prevent it from navigating
var header = document.getElementById('question-header');
var headerLink = header.getElementsByTagName('a')[0];
// when you pass code directly to console, page is (in most cases) already loaded
// you don't need to add listnener to 'load' event
headerLink.addEventListener(
'click',
function (e) {
document.getElementsByName('q')[0].value = nextItem();
e.preventDefault(); // prevent link to navigate
}
);
document.getElementsByName('q').value = arr[0]; // initial value
这是错误的(应该会给出一个错误),因为document.getElementsByName('q')
是一个没有value
属性的nodeList(类似于数组)。很可能只有一个元素名q
,所以可以使用[0]
来获得第一个(也是唯一一个)元素名。
document.getElementsByName('q')[0].value = arr[0];
// ...
document.getElementsByName('q')[0].value = nextItem();
我建议将这个元素存储在一个变量中,这样你就不会每次都加载它,而只加载一次。
var el = document.getElementsByName('q')[0];
// ...
el.value = arr[0];// or nextItem();
这是有效的:
HTML
<div id="header">
</div>
<form>
<input type="text" id="search" size="16" maxlength="16" />
</form>
JS
var arr = ['foo', 'bar', 'baz'];
var i = 0;
var search = document.getElementById('search');
function nextItem() {
i = i + 1; // increase i by one
i = i % arr.length; // if we've gone too high, start from `0` again
return arr[i]; // give us back the item of where we are now
}
window.addEventListener('load', function () {
search.textContent = arr[0]; // initial value
document.getElementById('header').addEventListener(
'click', // we want to listen for a click
function (e) { // the e here is the event itself
search.value = nextItem();
}
);
});
CSS
#header{
width:500px;
background-color:#444444;
color:#fff;
text-align:center;
padding-top:40px;
padding-bottom:40px;
}
FIDDLE
- 搜索api在mac上显示对话框
- 谷歌水印未显示在自定义搜索框中
- 使用API在我的网站Google-Map上显示搜索地址的纬度和经度
- 检查搜索结果是否存在多次如果是,则在Javascript中只显示一个结果
- 显示可链接的搜索结果+对齐方式
- 如何获取显示在谷歌搜索右侧的图像链接
- 在谷歌地图上显示所有搜索到的位置结果
- 使自动完成可搜索下拉菜单don'我不想在点击搜索时显示列表
- 为自定义网站创建JavaScript搜索框,创建显示搜索结果的弹出窗口
- 搜索XML文件并使用javascript显示结果
- Google Places API显示雷达搜索中的位置图标/图像
- 一页主题搜索/用下一个按钮突出显示文本
- 自动完成搜索无法正常工作,请不要迭代$中的所有元素.每个ul-li都会及时显示响应
- 使用 jQuery UI 折叠面板显示 couchDB 数据库搜索结果时出现问题
- 不显示 jsTree 搜索文本框
- 我可以配置默认的角度过滤器以显示字符串,以便以搜索词开头的字符串排在第一位
- 如何在 jQuery 中显示“未找到结果”列表搜索
- 显示移动窗口时引导导航栏中的 atach 搜索栏
- 如何隐藏基于先前搜索显示的先前表行
- jQuery智能搜索显示/隐藏内容元素