2016-11-16 19 views
0

В изображении додзе/диджита есть одно изображение. Я хочу установить событие click для изображения, но оно не может поймать событие.Как получить объекты dom в dijit Содержимое ContentBane

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> 

ответ

0

Edited.

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'); 
 
});
<script src="//ajax.googleapis.com/ajax/libs/dojo/1.10.4/dojo/dojo.js"></script> 
 
<div id="targetID">I get replaced.</div>

+0

В ContentPane имеются объекты объекта и объекта привязки. Поэтому я ожидаю, что поймаю событие привязки кликов и событие клика изображения. –

+0

Я отредактировал код, чтобы теперь было добавлено оповещение. –

0

Вы определяете функцию clickHandler внутри функции требуется. Это означает, что он не будет доступен после возврата. На консоли вы можете увидеть ошибку при щелчке изображения: «clickHandler не определен». Вы можете легко решить эту проблему, указав функцию clickHandler вне require().

<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>