当前位置: 代码迷 >> JavaScript >> 通过ajax获取的数据,怎么让其它js能执行
  详细解决方案

通过ajax获取的数据,怎么让其它js能执行

热度:429   发布时间:2013-12-20 17:03:19.0
通过ajax获取的数据,如何让其它js能执行?
通过ajax读取数据
 $(document).ready(function () {
//ajax读取数据并设置把相应的UL中的LI 某个ID上,

$.ajax({
            type: "get",
            dataType: "html",
            url: '/Ashx/act.ashx',
            data: sql,
 success: function (data) {
                $("#" + showul).html(data)
            },
});
});


================
需要执行的JS代码
$('li.caption').hover(function () {
            $(".cover", this).stop().animate({ top: '0px' }, { queue: false, duration: 160 });
        }, function () {
            $(".cover", this).stop().animate({ top: '-307px' }, { queue: false, duration: 160 });
        });

如果把数据直接写在页面上,上面这JS代码可以执行,但如果数据是通过ajax读取后再附加到页面上,而这块JS代码起不到作用,请教这要如何解决。谢谢。

------解决方案--------------------
引用:

$(document).on("hover","li.caption",function () {
            $(".cover", this).stop().animate({ top: '0px' }, { queue: false, duration: 160 });
        }, function () {
            $(".cover", this).stop().animate({ top: '-307px' }, { queue: false, duration: 160 });
        });

hover 改成 mouseover
------解决方案--------------------
jquery 的ajax把
<script scr='x'></script>封装好了。


// Install script dataType
jQuery.ajaxSetup({
accepts: {
script: "text/javascript, application/javascript, application/ecmascript, application/x-ecmascript"
},
contents: {
script: /(?:java
------解决方案--------------------
ecma)script/
},
converters: {
"text script": function( text ) {
jQuery.globalEval( text );
return text;
}
}
});

// Handle cache's special case and global
jQuery.ajaxPrefilter( "script", function( s ) {
if ( s.cache === undefined ) {
s.cache = false;
}
if ( s.crossDomain ) {
s.type = "GET";
s.global = false;
}
});

// Bind script tag hack transport
jQuery.ajaxTransport( "script", function(s) {

// This transport only deals with cross domain requests
if ( s.crossDomain ) {

var script,
head = document.head 
------解决方案--------------------
 jQuery("head")[0] 
------解决方案--------------------
 document.documentElement;

return {

send: function( _, callback ) {

script = document.createElement("script");

script.async = true;

if ( s.scriptCharset ) {
script.charset = s.scriptCharset;
}

script.src = s.url;

// Attach handlers for all browsers
script.onload
 = script.onreadystatechange = function( _, isAbort ) {

if ( isAbort 
------解决方案--------------------
 !script.readyState 
------解决方案--------------------
 /loaded
------解决方案--------------------
complete/.test( script.readyState ) ) {

// Handle memory leak in IE
  相关解决方案