javascript - .on 如何为 $(document) 工作

标签 javascript jquery

我的 JS 代码不适用于具有从 ajax 调用加载到 php 端的表单的模式。

我想我必须像下面这样调用函数:

$(document).on('click', '#tags', function ()

我应该如何将原始代码改成这样,谁能帮我解释一下吗?

$('#tags').on({  click: function(e) {
	e.preventDefault();
	alert('geklickt');},
	mouseenter: function(e) {
		alert('mouse enter!');
	}
});

仅使用 $('#tags') 不起作用。

此外,我必须更改此代码:

$(document).ready(function() {

    bookIndex = 0;

$('#bookForm')

    // Remove button click handler
    .on('click', '.removeButton', function() {
        var $row  = $(this).parents('.form-group'),
            index = $row.attr('data-book-index');

        // Remove fields
        $('#bookForm')
            .formValidation('removeField', $row.find('[name="book[' + index + '].title"]'))
            .formValidation('removeField', $row.find('[name="book[' + index + '].isbn"]'))
            .formValidation('removeField', $row.find('[name="book[' + index + '].price"]'));

        // Remove element containing the fields
        $row.remove();
    });
});

我必须更改哪些部分才能使用 document.on 支持?

最佳答案

只是pass string selector :

$(document).on({
  click: function(e) {
    e.preventDefault();
    alert('geklickt');
  },
  mouseenter: function(e) {
    alert('mouse enter!');
  }
}, '#tags');

关于javascript - .on 如何为 $(document) 工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39788986/

相关文章:

javascript - setTimeout 将参数从另一个函数传递给函数

javascript - 将 Canvas 的一小部分转换为 png

javascript - 如何在两个div之间切换?

javascript - 使用 Greasemonkey 将类添加到 iframe 的主体

javascript - 在 ionic v2 中单击按钮切换段

javascript - 使用 fullPage.js 中的 'afterRender' 回调来运行带有 jQ​​uery 事件的代码

javascript - 在其他函数中响应组件异步等待

javascript - 如何使用javascript滚动html中pdf对象元素的底部?

javascript - Cordova - jquery mobile img 路径

javascript - bootstrap 滚动 spy 偏移高度在 Firefox 中不起作用