ajax返回的数据,不再执行初次载入的JS,应该怎么改,先谢谢了!

WBOY
Release: 2016-06-23 13:29:51
Original
990 people have browsed it

点击一级栏目1后,

一级栏目1
变为:
一级栏目1
,

同时:id="xlm2"的li能显示二级栏目:
二级栏目1
,再点击二级栏目时,
二级栏目1
应该变为变为:
二级栏目1
才对,但是没有效果,dd没有加上样式 class="selected",

应该是$(document).ready(function()没有被执行,应该怎么改?先谢谢兄弟了!


JS:
$(document).ready(function(){   $("#select1 dd").click(function () {        bid=$(this).attr("id");        $(this).addClass("selected").siblings().removeClass("selected");	        $.ajax({             type:"GET",             url:"t.php",             data:"cid="+bid,             cache:false,             success:function(data){ 		$("#xlm2").show();                $("#xlm2").html("<dl id='select2'>"+data+"</dl>");             }         });   $("#select2 dd").click(function () {        $(this).addClass("selected").siblings().removeClass("selected");   }}
Copy after login



html:
<li class="list">   <dl id="select1">      <dd id="1">一级栏目1</dd>      <dd id="2">一级栏目2</dd>      <dd id="3">一级栏目3</dd>      <dd id="4">一级栏目4</dd>      <dd id="5">一级栏目5</dd>   </dl></li><li class="list" id="xlm2" style="display:none;"></li>
Copy after login



t.php
$cid = $_GET['cid'];$mesql=$empire->query("select * from class where classid=".$cid );while($r=$empire->fetch($mesql)){      $classname.= "<dd id=".$r[classid]." class=''>".$r[classname]."</dd>";}echo $classname; 
Copy after login



回复讨论(解决方案)

如果你的 jQuery 是 1.8.3 及以下
$("#select2 dd").live('click', function () {

如果的的 jquery 是 1.8.0 及以上
$("#select2 dd").on('click', function () {
这个写法要求 #select2 dd 是已存在的,否则应写作
$(document).on('click', "#select2 dd", function () {

哦,原来是这样,非常感谢,已经OK了,回答又快又准又好,非常感谢!谢谢!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template