是否有一种方法可以跟踪对HTML元素的所有更改
Is there a way to track all change to an HTML element?
是否有一种(好的)方法来跟踪HTML元素的所有更改?
我尝试使用javascript与jQuery,但它不工作。
$('div.formSubmitButton input[type="submit"]').change(function(event){
alert(event);
});
在提交按钮上设置了一个样式属性,但我找不到它是在哪里以及如何完成的。
编辑:我的问题不是jQuery特有的
您可以使用mutationobserver:
跟踪对DOM元素所做的更改。// 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);
http://jsfiddle.net/2VwLa/这将为您提供一个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"
只是一个示例,您可能还希望包含您的密码和电子邮件字段。
相关文章:
- 我应该怎么做才能避免在网页上移动元素(html、css、jQuery)
- 更改元素HTML,但忽略最后一个子项
- 增加在选择元素HTML中搜索的时间
- Jquery无法获取元素html
- 从所选元素 html 上方的元素获取属性值
- 在 JS 的单选按钮元素 (HTML) 中使用 title 属性
- 如何在页面上播放每个音频元素(HTML,JQuery,Javascript)
- 我需要一种新的方法来检测元素 HTML 是否有更改
- 获取父元素 html 以及子内容
- AngularJS指令:如何在ng-repeat中动态更改元素html
- html 属性中的引号被标记为 ”来自元素.html()的结果
- 在指令's链接中替换angular元素html
- 如果元素有子元素html元素
- 如何下推表格中的元素- HTML
- 如何在纯javascript中使用变量插入数组值,如元素HTML
- jQuery textilate .js -如何“重置”元素HTML并重新启动动画
- 将元素HTML附加到新附加的textarea中
- 在添加类的情况下,CSS 3的过渡是否不能与模板元素(HTML 5)一起工作?
- jQuery没有更新DOM元素HTML
- 在jQuery中更快地替换元素HTML