如何使用dojo amd添加onclick事件以突出显示?

时间:2015-01-08 21:10:48

标签: dojo

我是道场新手。我如何使用Dojo AMD试图找出答案 如何点击“oddF按钮”我可以为每个节点添加一个“奇数”类,我想删除类“蓝色”并添加类“红色”

    <!DOCTYPE HTML>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>Demo: Refresh a Node's Content</title>
           <link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/dojo/1.10.3/dijit/themes/claro/claro.css" media="screen">

           <style>
                .highlight {
                    background-color: yellow;
                }

                h3.condition {
                    border-top: 1px solid black;
                    padding:1em 0 !important;
                }

           </style>



</head>
<body class="claro">

<button class="oddF">dojo.query(".odd").forEach(...)</button><br />
<button class="evenAdd">dojo.query(".even").addClass("blue")</button><br />
<br />

  <div id="list">
    <div class="odd">One</div>
    <div class="even">Two</div>
    <div class="odd">Three</div>
    <div class="even">Four</div>
    <div class="odd">Five</div>
    <div class="even">Six</div>
</div>


    <script>dojoConfig = {async: true, parseOnLoad: false}</script>
   <script src="dojo/dojo.js"  data-dojo-config="async: true"></script>
   <script src="https://ajax.googleapis.com/ajax/libs/dojo/1.10.3/dojo/dojo.js"></script>

    <script>
   require([
    "dojo/query", 
    "dojo/request", 
    "dojo/dom-form" ,
    "dojo/dom-construct",
    "dojo/dom-style",
    "dojo/domReady!"],
  function(query,request,domForm,domConstruct,domStyle) {

    query(".oddF").on("click", function(e) {
        e.preventDefault();
        //var btn = e.target;

     dojo.query(".odd").forEach(function(node){
                    // For each ndoe with a class of "odd",
                    // remove the class "blue" and add the class "red"
                    dojo.removeClass(node, "blue");
                    dojo.addClass(node, "red");
        });



    });





  });
    </script>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

使用模块dojo / dom-class来操作节点的类。

  <script type="text/javascript">
   require(["dojo/query", "dojo/request", "dojo/dom-form",
            "dojo/dom-construct", "dojo/dom-style", "dojo/dom-class", "dojo/domReady!"],
    function(query,request,domForm,domConstruct,domStyle, domClass) {
     query(".oddF").on("click", function(e) {
      e.preventDefault();
      dojo.query(".odd").forEach(function(node){
        // For each node with a class of "odd",
        // remove the class "blue" and add the class "red"
        if (domClass.contains(node, "blue"))
          domClass.remove(node, "blue");
        domClass.add(node, "red");
      });
     });
    });
  </script>