在div中使用onchange

时间:2011-07-13 08:33:18

标签: javascript html onchange

是否可以在onchange

中使用<div>个事件

因为我的<div>更改了其内部文字。

我可以这样做吗?

<div id="name" onchange="calculateTotal();"></div> 

6 个答案:

答案 0 :(得分:16)

没有; onchange属性仅适用于表单字段元素(inputselecttextarea)。

答案 1 :(得分:5)

由于您说您正在使用AJAX,为什么不在更新文本时执行该功能。我不确定你是否使用任何库,但是如果是jQuery只是这样做:

 $.ajax({ ...,
          success: function() {
               ... other things ...
               ... setting div text ...
               calculateTotal();
          }
       });

答案 2 :(得分:4)

当您自己更改文本时,只需在AJAX调用完成并且文本已放入元素中时调用calculateTotal

示例(使用jQuery):

$('#name').load('GetFragment.php', calculateTotal);

答案 3 :(得分:1)

当用户更改输入值时调用Onchange。所以答案是肯定的,但它没有任何效果。 我假设您以编程方式更改内容,因此添加将调用此函数的简单代码

答案 4 :(得分:0)

您可以使用onblur事件。在表,div等上运行的Onblur事件。

func main() {
    cmd := exec.Command("G:\\go_workspace\\GOPATH\\src\\pjx\\modules\\exec\\exec")

    stdin, e := cmd.StdinPipe()
    if e != nil {
        panic(e)
    }

    stdout, e := cmd.StdoutPipe()
    if e != nil {
        panic(e)
    }
    if e := cmd.Start(); e != nil {
        panic(e)
    }
    _, e = stdin.Write([]byte("hello\nworld\n"))
    if e != nil {
        panic(e)
    }
    stdin.Close()

    out, _ := ioutil.ReadAll(stdout)
    // or you can use a loop
    //for {
    //  var buf = make([]byte, 512)
    //  n, e := stdout.Read(buf)
    //  if e == io.EOF {
    //      break
    //  }
    //  if e != nil {
    //      panic(e)
    //  }
    //  fmt.Println(string(buf[:n]))
    //}

    fmt.Println(string(out))

    if e := cmd.Wait(); e != nil {
        panic(e)
    }
}

答案 5 :(得分:0)

这对大多数Web设计人员的薪水来说有点高,但是dom监视div并不是什么问题。 它也是纯html,不需要任何内容​​。

最简单的方法是举一个例子。 Div是可编辑的,因此只需在其上单击并输入内容,您的JavaScript控制台就会显示正在发生的情况。 (如果您不了解如何使用javascript调试器,这可能对您来说很难,所以只需学习它即可;))

<html>
<head>
<script>
        // Run the code once the DOM is created, think jquery $(function(){}); but HTML5
        document.addEventListener("DOMContentLoaded", function () {
            const commandlineDiv = document.getElementById('commandline');
            function mutationCallback(mutationsList, observer) {
                console.log(mutationsList);
                console.log(observer);
                for (const 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.');
                    } else {
                        console.log('mutation.type: ' + mutation.type);
                        console.log('New value ' + JSON.stringify(mutation.target.data));
                    }
                }
            }
            // Create an observer instance linked to the callback function
            const observer = new MutationObserver(mutationCallback);
            // What to observe
            const mutationConfig = { attributes: true, childList: true, subtree: true, characterData: true };
            observer.observe(commandlineDiv, mutationConfig);
        })

</script>
</head>
<body>
<div id="commandline" contentEditable="true">commandline</div>
</body>
</html>
相关问题