jquery 更改时触发span text/html

vc6uscn9  于 2023-01-04  发布在  jQuery
关注(0)|答案(6)|浏览(186)

span标签text/html发生更改时,jQuery或JavaScript中是否会触发任何事件?
代码:

<span class="user-location"> </span>

$('.user-location').change(function () {
    //Not working
});
vatpfxk5

vatpfxk51#

你可以使用DOMSubtreeModified来跟踪你的span元素的变化,即(如果你的span元素的文本动态变化)。

$('.user-location').on('DOMSubtreeModified',function(){
  alert('changed')
})

查看以下链接https://jsbin.com/volilewiwi/edit?html,js,output

h9a6wy2h

h9a6wy2h2#

    • 简短答案适用于change-事件为jQuery**,

此事件仅限于input元素、textarea框和select元素。对于选择框、复选框和单选按钮,当用户用鼠标进行选择时立即激发该事件,但对于其他元素类型,该事件将推迟到元素失去焦点时激发。... * 此处是指向文档https://api.jquery.com/change/ * 的链接

    • 这里是一个简短的示例(改编自MDN参考)**
  • 在示例中,span更改是使用setTimeout模拟的 *
// select the target node
var target = document.getElementById('user-location');
 
// create an observer instance
var observer = new MutationObserver(function(mutations) {
  mutations.forEach(function(mutation) {
    console.info("EVENT TRIGGERT " + mutation.target.id);
  });    
});
 
// 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);

// simulate the Change of the text value of span
function simulateChange(){
    target.innerText = "CHANGE";
}

setTimeout(simulateChange, 2000);
<span id="user-location"></span>

如果你想/必须使用jQuery,你可以这样做:

  • 在此示例中,我添加了第二个span,只是为了演示它如何工作 *

一个二个一个一个

i7uaboj4

i7uaboj43#

使用Javascript变异观察器

//More Details https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver
 // select the target node
var target = document.querySelector('.user-location')
// create an observer instance
var observer = new MutationObserver(function(mutations) {
  console.log($('.user-location').text());   
});
// configuration of the observer:
var config = { childList: true};
// pass in the target node, as well as the observer options
observer.observe(target, config);
332nm8kg

332nm8kg4#

您可以使用input事件:
就像这样:

$(document).ready(function(){

    $(".user-location").on("input",function(){

        console.log("You change Span tag");

    })
})

示例:

<!DOCTYPE html>
<html>
    <head>
        <style>
            span {
                border: 1px solid #000;
                width: 200px;
                height: 20px;
                position: absolute;
            }
        </style>
    </head>
    <body>
        <span class="user-location" contenteditable="true"> </span>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <script>
    $(document).ready(function(){

        $(".user-location").on("input",function(){

            console.log("You change Span tag");

        })
    })
    </script>
    </body>  
</html>
6ioyuze2

6ioyuze25#

使用突变API MutationObserver

// Select the node that will be observed for mutations
var targetNode = document.getElementById('some-id');

// Options for the observer (which mutations to observe)
var config = { attributes: true, childList: true };

// Callback function to execute when mutations are observed
var callback = function(mutationsList) {
    for(var mutation of mutationsList) {
        if (mutation.type == 'childList') {
            console.log('A child node has been added or removed.');
        }
        else if (mutation.type == 'attributes') {
            console.log('The ' + mutation.attributeName + ' attribute was modified.');
        }
    }
};

// Create an observer instance linked to the callback function
var observer = new MutationObserver(callback);

// Start observing the target node for configured mutations
observer.observe(targetNode, config);

// Later, you can stop observing
observer.disconnect();
k10s72fa

k10s72fa6#

您可以使用javascript来实现这一点。

<html>
  <body>
    <span class="user-location" onchange="myFunction()">
       <input type="text"> 
    </span>
    <script>
       function myFunction() {
          alert("work");
       }
    </script>
 </body>
  </html>

希望能有所帮助。

相关问题