.jquery中的每个(函数(){})不处理动态创建的div元素
我正在尝试为动态创建的包含图像的div元素创建工具提示弹出框。 我正在使用一个名为jquery的jquery插件 我目前的脚本没有给我想要的结果.jquery中的每个(函数(){})不处理动态创建的div元素,jquery,html,popover,Jquery,Html,Popover,我正在尝试为动态创建的包含图像的div元素创建工具提示弹出框。 我正在使用一个名为jquery的jquery插件 我目前的脚本没有给我想要的结果 <!-- language: lang-js --> <script type="text/javascript"> $(document).ready(function () { $('.thumbnail-hoverable').each(function () {
<!-- language: lang-js -->
<script type="text/javascript">
$(document).ready(function () {
$('.thumbnail-hoverable').each(function () {
var mouseOnDiv = $(this);
var imgsrc = mouseOnDiv.find("img").attr("src").toString();
console.log(imgsrc);
var tipContent
$(function () {
tipContent = $('<div class="image-detail-popover"><div class="img-p
opover-detail-container" style="border: 2px solid #cccccc; margin-bottom: 10px; width: 370px; height: 322px;display: table-cell; vertical-align: middle;"><img alt="test" class="clickable" src = "" style="max-height: 322px; max-width: 370px; display: block; margin: auto;" /><img alt="" src="Images/fowto-watermark.png" style="position: absolute; top: 0; left: 0; z-index: 9999999;" /></div></div>');
tipContent.find("div.img-popover-detail-container").children("img").attr("src", imgsrc);
});
console.log(tipContent.toString());
mouseOnDiv.data('powertipjq', tipContent);
mouseOnDiv.powerTip({
//placement: 'n',
mouseOnToPopup: true,
});
$("img.clickable").click(function () {
$(parent.document).find(".ticket-img-container").children("img").attr("src", imgsrc);
alert("clicked");
console.log("clicked");
});
$("div.img-popover-detail-container").mouseenter(function () {
$(parent.document).find(".ticket-img-container").children("img").attr("src", imgsrc);
alert("clicked div");
console.log("clicked div");
});
});
});
</script>
$(文档).ready(函数(){
$('.thumbnail hoverable')。每个(函数(){
var mouseOnDiv=$(此);
var imgsrc=mouseOnDiv.find(“img”).attr(“src”).toString();
控制台日志(imgsrc);
var tipContent
$(函数(){
tipContent=$('');
tipContent.find(“div.img-popover-detail-container”).children(“img”).attr(“src”,imgsrc);
});
log(tipContent.toString());
mouseOnDiv.data('powertipjq',tipContent);
mouseOnDiv.powerTip({
//位置:'n',
老鼠崽:没错,
});
$(“img.clickable”)。单击(函数(){
$(parent.document).find(“.ticket img container”).children(“img”).attr(“src”,imgsrc);
警报(“点击”);
控制台日志(“单击”);
});
$(“div.img-popover-detail-container”).mouseenter(函数(){
$(parent.document).find(“.ticket img container”).children(“img”).attr(“src”,imgsrc);
警报(“点击div”);
console.log(“单击的div”);
});
});
});
类缩略图可悬停的div元素是使用asp中继器动态创建的。但是,每一个都不起作用。实际上它一直工作到console.log(imgsrc)。我必须用.mouseenter替换.each,但这会破坏Powertip插件的使用,因为它已经这样做了。此外,即使使用mouseenter,.click函数也不起作用。我需要单击popover中的一个元素来设置主页中的其他元素。有人能帮忙吗 你错了;在
var tipcontent
之后,代码有多个问题
tipcontent初始化后第一个缺少冒号
var tipcontent ;
^--- Missing colon
- 不分离关注点。(使您的JS更干净)李>
- 为什么要使用$.each来绑定事件
每个(function(){
,var tipContent
在没有的情况下打开;
和$(function(){
关于这一点?我想到了使用$。每个都是出于这个原因:带有类thumbnail hoverable的分区是通过asp repeater动态创建的,使用数据库表中的数据,其中一个是img元素的src attr的url。我想为创建的每个img元素提取此url,并将其传递给tipcontent,其中h将作为参数传递给工具提示。如果您能给我一个更好的方法,我将不胜感激。。。