如何在dijit ContentPane内容中获取dom对象

时间:2016-11-16 12:57:59

标签: dojo

dojo / dijit的内容中有一个图像。我想为图像设置click事件,但它无法捕获事件。

the code in JSFiddle

             

    <script>dojoConfig = {parseOnLoad: true}</script>
    <script src='../../_static/js/dojo/dojo.js'></script>

    <script>
require(["dijit/layout/ContentPane", "dojo/domReady!"], function(ContentPane){
    new ContentPane({
      content:"<p><img src='https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/logo_white_fe6da1ec.png' onclick='clickHandler()' /></p>",
      style:"height:125px"
    }, "targetID").startup();

function clickHandler()
{
alert("img clicked");
}
});
    </script>
</head>
<body class="claro">
    <div id="targetID">
    I get replaced.
</div>
</body>
</html>

2 个答案:

答案 0 :(得分:0)

编辑。

&#13;
&#13;
require([
  'dojo/dom-construct',  
  'dijit/layout/ContentPane',
  'dojo/domReady!'
], function(domConstruct, ContentPane){
  new ContentPane({
    content: '<div id="imageDiv"></div>',
    style: 'height:125px'
  }, 'targetID').startup();
  domConstruct.create('img', {
    src: 'https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/logo_white_fe6da1ec.png',
    onclick: function(){ alert('i have been clicked') }
  }, 'imageDiv');
});
&#13;
<script src="//ajax.googleapis.com/ajax/libs/dojo/1.10.4/dojo/dojo.js"></script>
<div id="targetID">I get replaced.</div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

您正在require函数中定义clickHandler函数。这意味着在require返回后它将不可用。在控制台上,您可以在单击图像时看到错误:“clickHandler未定义”。您可以通过在require()之外定义clickHandler函数来轻松解决此问题。

<script>
require(["dijit/layout/ContentPane", "dojo/domReady!"], function(ContentPane){
    new ContentPane({
      content:"<p><img src='https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/logo_white_fe6da1ec.png' onclick='clickHandler()' /></p>",
      style:"height:125px"
    }, "targetID").startup();

});
function clickHandler()
{
alert("img clicked");
}
</script>