【发布时间】:2013-01-27 03:28:40
【问题描述】:
我通过将 html 从我的控制器回显到 ajax 函数来创建一个列表。所以我的javascript通过ajax获取这些数据并创建li的......然后我想从这个创建的html中调用一个属性......它不起作用。 ...我做错了什么?
JAVA 脚本
//创建列表
$.ajax({
url:'ol_locations', // ol_locations is a controller, see below.
dataType: 'html',
success: function (data3){
// list elements in the ordered list
$('ol.locations_list').html(data3);
}});
//list populated successfully
控制器 ol_location ....
公共函数 ol_locations(){
$this->load->model('model_location');
$data = $this ->model_location -> fetch_location_data();
$i=1;
foreach ($data as $row){
if ($row->Type == 'locality'){
echo "<div data-color ='red' id='".$i."'><li><a href ='#'> <span class='location_title'>". $row -> Nickname . " </span> ". $row->FormattedAddress .
"</a> <a class='remove_location' title='Remove this location' data-nid='{$row->Id}'
style='float:right;'>x</a>" ." </li> </div>";
$i++;
}
else {
echo "<div data-color ='red' id='".$i."'><li> <a href ='#'><span class='location_title'>". $row -> Nickname. " ". $row -> Radius ." km of </span> ". " </span> ". $row->FormattedAddress ."</a><a class='remove_location' title='Remove this location' data-nid='{$row->Id}'style='float:right;'>x</a>" ." </li> </div>";
$i++;
} }
}
现在 HTML 呈现良好...但是当我尝试访问此“创建的 html”中的任何元素时,没有任何反应...
例如 alert ($('#1').attr('color'));将给出“null”或未定义。
我尝试了很多类似的东西
var initial_location = $('ol.locations_list li a'). attr('href');
仍然为空...
我真的被困在这里了……
【问题讨论】:
-
我没有看到
$("ol.locations_list"),应该是$("#1").data('color')或$("#1").attr('data-color') -
控制台您的 ajax 数据并在 google chrome 浏览器中打开控制台 (ctrl+alt+j) 然后查看您收到的 html...以下是控制台的代码....console.log( $('ol.locations_list').html())
-
类似问题及其解决方法:stackoverflow.com/questions/6958851/…
-
HTML 无效...
LI不能是DIV的子级,DIV不能是OL的子级。浏览器在无法将其解析为写入时,通常会移动无效的 html。使用 w3c 验证器检查它 -
html 是否真的被添加到 DOM 中?
标签: php javascript jquery ajax