javascript click()方法的问题/ html
Trouble with javascript click() method / html
提前感谢你看这个。
我的webapp允许用户从四个不同的下拉菜单中选择选项。当用户进行选择时,程序成功执行以下click()
函数,该函数在div
元素中创建一个新的span
元素:
var activeFilterNames = [];
$('.filter').click(function()
{
if (!this.classList.contains('disabled'))
{
//above checks to see if link has already been clicked
//and is therefore disabled. If not, go ahead.
activeFilterNames.push(this.textContent);
//above adds name of selected link to array
i = activeFilterNames.length;
var newFilter = document.createElement('span');
newFilter.id = activeFilterNames[i-1];
newFilter.className = this.className+" activated";
//above adds a new class to identify the filter as 'activated'
//above retains existing classname to identify
//which of the four filters it came from
newFilter.innerHTML = activeFilterNames[i-1];
//above creates display text to be rendered in browser
document.getElementById('active_filters').appendChild(newFilter);
//above is the div in which span will be appended to other spans.
$("#active_filters > span").removeClass('filter');
//above removes .filter class so that this newly created span does
//not respond to the .filter click() function.
$(this).addClass('disabled');
//above adds 'disabled' class to the originally
//clicked link, causing it to skip this block of code
}
}
);
到目前为止,一切似乎都很好(尽管我可能遗漏了一些东西)。实际上,我创建的span
元素看起来像这样在html:
<span id="id_name" class="menu_name activated">Rendered Name</span>
并且由于上面的span没有filter
类,然后我尝试在javascript中创建一个新函数(只是作为测试)以使元素响应:
$('.activated').click(function()
{
alert('hi');
}
);
但是运气不好。我试图通过在span
内嵌套div
或a
来重新渲染动态创建的元素,根据需要修改代码,但仍然没有。我想保留span
,因为这是我发现将这些动态生成的元素包装到div
(#active_filters
)中创建它们的第二行的唯一方法。
有人能看到我做错了什么,因为我想让activated
点击()函数响应每个新创建的span
元素?
如果您试图在创建$('.activated')中包含的DOM元素之前绑定它们,则绑定将无法工作。这通常意味着您需要在创建之后绑定事件侦听器。如果要动态创建DOM元素,则需要这样做:
var activeFilterNames = [];
$('.filter').click(function()
{
if (!this.classList.contains('disabled'))
{
activeFilterNames.push(this.textContent);
i = activeFilterNames.length;
var newFilter = document.createElement('span');
newFilter.id = activeFilterNames[i-1];
newFilter.className = this.className+" activated";
newFilter.innerHTML = activeFilterNames[i-1];
document.getElementById('active_filters').appendChild(newFilter);
$('.activated').unbind('click');
$('.activated').click(function()
{
alert('hi');
}
);
$("#active_filters > span").removeClass('filter');
$(this).addClass('disabled');
}
}
);
注意,在绑定之前我们取消绑定。这可以确保如果多次执行此操作,不会将2、3、4次绑定到同一个DOM元素。
您需要在动态创建的元素上附加click事件。在jQuery中,这可以通过on
方法来完成,如果你将选择器作为第二个参数传递给父元素,并将click附加到body
上,例如:
$( 'body' ).on( 'click', '.activated', function()
{
alert('hi');
}
);
相关文章:
- 使用Handontable在同一列中用纯文本和html对问题进行排序
- 在HTML中调用函数时出现问题
- 在将javascript附加到我的html中时遇到问题
- 使用ajax在html中加载html文件时出现的问题
- 将HTML表单发布到iframe会导致浏览器历史记录出现问题
- 使用HTML和JS的iPhone游戏中的性能问题,以及appMobi
- Html、css和jQuery.我的代码有问题
- jQuery-数据提取问题(html遍历)
- ajax html javascript页面刷新无闪烁问题
- html单选按钮检查问题
- c# mvc nreco HtmlToPdfConverter 问题将 html 文档转换为 pdf
- CSS 溢出的滚动问题:滚动 HTML
- 在我的Javascript和HTML之间的表单提交按钮链接中遇到问题
- 跨域问题:HTML
- Ckeditor出现格式错误的html问题
- 创建/使用简单的美制到公制转换器的问题.(HTML/JavaScript)函数未启动
- 简单的php字符串问题(HTML和javascript)
- 表对齐问题HTML JavaScript
- javascript click()方法的问题/ html
- JavaScript验证问题——HTML表单即使不完整也要提交