Jquery 将数据从变量数据索引传递到模式框
我试图通过数据索引将数据传递到引导模式框。下面给出了我的html标记和javascript代码片段。我不知道如何从变量中填充图像、超链接和标题Jquery 将数据从变量数据索引传递到模式框,jquery,twitter-bootstrap,modal-dialog,Jquery,Twitter Bootstrap,Modal Dialog,我试图通过数据索引将数据传递到引导模式框。下面给出了我的html标记和javascript代码片段。我不知道如何从变量中填充图像、超链接和标题 function showModal(a) { var id = $(this).attr("data-index"); $("#modal-photo").html('<img src=""/>'); $("#modal-title").html('<a href="">description goes
function showModal(a) {
var id = $(this).attr("data-index");
$("#modal-photo").html('<img src=""/>');
$("#modal-title").html('<a href="">description goes here</a>');
$('#myModal').modal('show')
}
<div class='post' data-index='1'>
<img src="">
<a class="desc" href="">description goes here</a>
</div>
<div class='post' data-index='2'>
<img src="">
<a class="desc" href="">description goes here</a>
</div>
<div class='post' data-index='3'>
<img src="">
<a class="desc" href="">description goes here</a>
</div>
功能显示模式(a){
var id=$(this.attr(“数据索引”);
$(“#模态照片”).html(“”);
$(“#模式标题”).html(“”);
$('#myModal').modal('show'))
}
编辑:
我正在考虑将描述封装到下面代码的段落标记中。我不知道这是不是有效的方法
function showModal(a) {
var photo = $("img", $(this).parent()).attr("data-index");
var title = $("p", $(this).parent()).attr("data-index");
$("#modal-photo").html('<img src="'+photo+'"/>');
$("#modal-title").html('<a href="">'+title+'</a>');
$('#myModal').modal('show')
}
功能显示模式(a){
var photo=$(“img”,$(this.parent()).attr(“数据索引”);
var title=$(“p”,$(this.parent()).attr(“数据索引”);
$(“#模态照片”).html(“”);
$(“#模式标题”).html(“”);
$('#myModal').modal('show'))
}
但超链接对我仍然不可用:D试试这个
$('.open-it').on('click', showModal);
function showModal(e) {
var index = $(this).parent().data('index');
console.log(index);
//do stuff
}
此
不会引用示例中的链接。它将指向窗口
对象
上面的代码假设
的父级。打开它
包含数据。如果看不到更多代码,很难判断。请尝试使用数据属性。试试看这里:
基本上,您将要做的是将您的数据提供给data atribute,然后使用
var yourContent=$('#link')。数据('content') 是什么触发了('click',function(){showmodel($(this).attr('data-index'))}