简体   繁体   English

当属性值使用jQuery或JavaScript更改时如何执行动作?

[英]How do I execute an action when an attribute value changes using jQuery or JavaScript?

I have a div that contains the attribute aria-hidden . 我有一个div ,其中包含aria-hidden属性。 I would like to set up a listener or execute an action each time the attribute value dynamically changes. 每当属性值动态更改时,我想设置一个侦听器或执行一个动作。

My div is 我的div是

<div class="slide2" aria-hidden="*"></div>

The value for "aria-hidden" could either be true or false. “ aria-hidden”的值可以为true或false。

My code is: 我的代码是:

$(document).ready(function () {
   $(".slide2").bind('change', function(event) {
     if ( $('.slide2').attr('aria-hidden') == 'false' ) {
        alert('do something');
     }
   });
});

I have read examples using MutationObserver but that examples I've seen display an action being executed when ANY element attribute changes. 我已经阅读了使用MutationObserver示例,但是我看到的示例显示了在ANY元素属性更改时正在执行的操作。 I would like to see an example showing that when a specific div ( <div class="slide2"></div> ) changes an action is executed. 我想看一个示例,该示例显示当特定div( <div class="slide2"></div> )更改时执行的操作。

Explanation 说明

Inside of the observer you have to check the attribute you want to change. observer内部,您必须检查要更改的attribute I did it with pure javascript ( mutation.target.getAttribute('aria-hidden') ). 我是用纯Javascript做到的( mutation.target.getAttribute('aria-hidden') )。
With the attributeFilter you can choose specific attributes to observe. 使用attributeFilter您可以选择要观察的特定属性。 In your case aria-hidden . 就您而言, aria-hidden

The Important Part 重要部分

var slideObserver = new MutationObserver(function(mutations) {
  mutations.forEach(function(mutation) {
    if (mutation.type == "attributes") {
      // here you can play with it now :)
      if (mutation.target.getAttribute('aria-hidden') == "true") {
        console.log('ok. aria-hidden is true') 
      } else {
        console.log(':s aria-hidden is false') 
      }      
    }
  });
});

Example

 var MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver var slide = document.querySelector('.slide2') document.querySelector('#btn').addEventListener('click', toggleAriaHiddenRole) var slideObserver = new MutationObserver(function(mutations) { mutations.forEach(function(mutation) { if (mutation.type == "attributes") { // here you can play with it now :) if (mutation.target.getAttribute('aria-hidden') == "true") { console.log('ok. aria-hidden is true') } else { console.log(':s aria-hidden is false') } } }); }); slideObserver.observe(slide, { attributes: true, //configure it to listen to attribute changes attributeFilter: ['aria-hidden'] // filter your attributes }); function changeAriaHiddenRole(element, boolean) { element.setAttribute('aria-hidden', boolean) } function toggleAriaHiddenRole(boolean) { if (slide.getAttribute('aria-hidden') === "true") { changeAriaHiddenRole(slide, false) } else { changeAriaHiddenRole(slide, true) } } 
 <div class="slide2" aria-hidden="false"></div> <button id="btn">toggle aria-hidden</button> 

It works for me after I put the js code at the bottom of the file. 在我将js代码放在文件底部后,它对我有用。 The possible error occurs should be: 可能出现的错误应该是:

parameter 1 is not of type 'Node' 参数1的类型不是“节点”

That is because the listener was executed before the DOM was read, so obviously, it has to be loaded before you call observe(node, config); 那是因为侦听器是在读取DOM之前执行的,因此很明显,必须在调用observe(node, config);之前加载它observe(node, config);

here is one suggestion: 这是一个建议:

<body>
<div class="slide2" aria-hidden="*"></div>
<script>
$(document).ready(function () {
   $(".slide2").bind('change', function(event) {
     if ( $('.slide2').attr('aria-hidden') == 'false' ) {
        alert('do something');
     }
   });
});
</script>
</body>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 jquery 和 javascript 获取类属性值 - How do I get the class attribute value using jquery and javascript 告诉jQuery执行value属性中设置的动作 - Tell jQuery to execute the action set in the value attribute 当不是默认表单操作时,如何使用JavaScript或jQuery通过Enter键提交表单 - How do I use JavaScript or jQuery to submit a form by using the Enter key, when not the default form action 如何使用Jquery或Javascript在.CSS文件中进行更改? - How Do I make Changes in .CSS File Using Jquery Or Javascript? 如何使用javascript获取元素的自定义属性值 - How do I get the custom attribute value of an element using javascript 当按钮到达使用 jQuery 的 Web 浏览器屏幕顶部时,您如何执行操作? - How do you execute an action when a button reaches the top of a web browser screen using jQuery? 当div值改变时执行javascript函数 - Execute javascript function when div value changes 如何使用AngularJS监视jQuery修改的属性值的变化 - How to watch for changes in attribute value modified by jQuery, using AngularJS 当HTML文本框的值由于document.getElementById(“ textfield”)。value =“”而改变时,如何执行Javascript函数? - How can I execute a Javascript function when the value of a HTML textbox changes due to document.getElementById(“textfield”).value = “”; jQuery 获取属性值,即使它发生变化 - jQuery Get Attribute Value even when it changes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM