Javascript 使用jquery获取动态创建的元素的内容
使用服务器端进程,我已经创建了我的表,并在一个元素(如bellow)中加载了一些数据 我想要类的内容更多元素,我尝试使用ready,load,但在发出警报时我没有定义,所以请有人帮助我 我的全部密码都在这里Javascript 使用jquery获取动态创建的元素的内容,javascript,php,jquery,Javascript,Php,Jquery,使用服务器端进程,我已经创建了我的表,并在一个元素(如bellow)中加载了一些数据 我想要类的内容更多元素,我尝试使用ready,load,但在发出警报时我没有定义,所以请有人帮助我 我的全部密码都在这里 <div class="container margin_120" id="form1"> <div class="row"> <div class="col-lg-12 col-md-12 col-xs-12"
<div class="container margin_120" id="form1">
<div class="row">
<div class="col-lg-12 col-md-12 col-xs-12">
<div id="tools">
<div class="row">
<div class="col-md-12 col-sm-12 col-xs-12">
<h4 class="inner margin_5_tb"><i class=""></i>View Counts of Students Needing Financial Assistance<button type="button" id="button1" style="border:none;background-Color:#EAECEE; float: right;font-size:12px" ><u>Hide</u></button></h4>
</div>
</div>
</div><!--/tools -->
<div class="form-group" id="countTableDes">
<table class="table table-bordered" id="loan_data">
<thead>
<tr>
<th>Student Name</th>
<th>Student Photo</th>
<th>some data</th>
</tr>
</thead>
</table>
</div>
</div>
</div>
</div>
<script src="<?php echo base_url(); ?>assets/js/jquery-1.11.2.min.js"></script>
$(document).ready(function(){
var dataTable = $('#loan_data').DataTable({
"processing":true,
"serverSide":true,
"autoWidth": false,
"order":[],
"ajax":{
url:'<?php echo site_url("home/getLoans"); ?>',
type:"POST"
},
"columns":[
{ 'data' : 'stu_firstName' },
{ 'data' : 'id',
'render':function(id,data,row){
if(row.stu_passportPhoto && row.stu_passportPhoto!=null){
return '<p class="more"><b>About me</b> <br/>'+row.stu_aboutStudent+'</p>';
}
}
},
{
'data' : 'stu_somedata'
},
]
});
//Here I want to get each more class content.
$(document).find(".more").each(function(){
var content = $(this).text();
alert(content);
});
});
</script>
查看需要经济资助的学生的计数隐藏
学名
学生照片
一些数据
问题可能是datatable的呈现方式以及column:render的执行方式
最坏的情况是使用一个mutator或一个事件侦听器
例如arrival.js
这里是链接
它将侦听新创建的元素,您可以在其函数中运行某些内容
这样做,
$(document).arrive(".more", function() {
// 'this' refers to the newly created element
var content = $(this).text();
alert(content); /* do something */
});
由于类为“more”的元素是由DataTable动态创建的,因此必须等待表或至少行完全绘制完成。为此,您必须挂接DataTables提供的一个可用回调:
哪一个完全取决于您的需要,大概drawCallback
适用于您的案例,或者您也可以对每一行分别使用createdRow
这是文档中的示例片段,用于说明您的案例:
$('#loan_data').dataTable( {
// rest of your options goes here...
"drawCallback": function( settings ) {
// you'll want to select only inside your table,
// not the whole document, for performance reasons:
$('#loan_data').find(".more").each(function(){
var content = $(this).text();
alert(content);
});
}
} );
如果使用浏览器检查器工具查看表,是否看到HTML中的p.more
元素?是的,它就在那里。.DataTable()
调用是否在$(文档).ready()中
函数,并在您的p.more
选择之前执行。是的,这是仅流动态创建的元素,某些时候不能通过类名直接调用。您必须通过root访问它们。因此,不要使用$(“.more”)而是使用$(document).find(“.more”).each。