有没有一种(好的)方法来跟踪HTML元素的所有更改?
我尝试使用JavaScript和jQuery,但它不起作用。
$('div.formSubmitButton input[type="submit"]').change(function(event){
alert(event);
});
一些提交按钮上设置了样式属性,但我找不到它是在哪里和如何设置的。
编辑:我的问题与jQuery无关。
有没有一种(好的)方法来跟踪HTML元素的所有更改?
我尝试使用JavaScript和jQuery,但它不起作用。
$('div.formSubmitButton input[type="submit"]').change(function(event){
alert(event);
});
// select the target node
var target = document.querySelector('div.formSubmitButton input[type="submit"]');
// create an observer instance
var observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
console.log(mutation);
});
});
// configuration of the observer:
var config = { attributes: true, childList: true, characterData: true }
// pass in the target node, as well as the observer options
observer.observe(target, config);
这将为您提供一个MutationRecord对象,其中包含有关更改的详细信息。有关变异的更多信息,请参见:https://hacks.mozilla.org/2012/05/dom-mutationobserver-reacting-to-dom-changes-without-killing-browser-performance/
你可以跟踪输入字段的更改或在提交时进行检查:
$('form').submit(function(event){
alert("this gets called when form submitted here you can test differences");
});
$('form input[type="text"]').change(function(event){
alert("this gets called when and text input field gets changed");
});
$('form input[type="text"]').keydown(function(event){
alert("this gets called on key board input before the data is inserted in input field");
});
$('form input[type="text"]').keyup(function(event){
alert("this gets called on key board input after the data is inserted in input field");
});
type="text"
只是一个示例,您可能还希望包括密码和电子邮件字段(如果使用 on change 事件,则还要包括选择框)。