读书人

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

发布时间: 2013-12-20 17:03:19 作者: rapoo

通过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


script.onload = script.onreadystatechange = null;

// Remove the script
if ( script.parentNode ) {
script.parentNode.removeChild( script );
}

// Dereference the script
script = null;

// Callback if not abort
if ( !isAbort ) {
callback( 200, "success" );
}
}
};

// Circumvent IE6 bugs with base elements (#2709 and #4378) by prepending
// Use native DOM manipulation to avoid our domManip AJAX trickery
head.insertBefore( script, head.firstChild );
},

abort: function() {
if ( script ) {
script.onload( undefined, true );
}
}
};
}
});




var oldCallbacks = [],
rjsonp = /(=)\?(?=&
[解决办法]
$)
[解决办法]
\?\?/;

// Default jsonp settings
jQuery.ajaxSetup({
jsonp: "callback",
jsonpCallback: function() {
var callback = oldCallbacks.pop()
[解决办法]
( jQuery.expando + "_" + ( ajax_nonce++ ) );
this[ callback ] = true;
return callback;
}
});

// Detect, normalize options and install callbacks for jsonp requests
jQuery.ajaxPrefilter( "json jsonp", function( s, originalSettings, jqXHR ) {

var callbackName, overwritten, responseContainer,
jsonProp = s.jsonp !== false && ( rjsonp.test( s.url ) ?
"url" :
typeof s.data === "string" && !( s.contentType
[解决办法]
"" ).indexOf("application/x-www-form-urlencoded") && rjsonp.test( s.data ) && "data"
);

// Handle iff the expected data type is "jsonp" or we have a parameter to set
if ( jsonProp
[解决办法]
s.dataTypes[ 0 ] === "jsonp" ) {

// Get callback name, remembering preexisting value associated with it
callbackName = s.jsonpCallback = jQuery.isFunction( s.jsonpCallback ) ?
s.jsonpCallback() :
s.jsonpCallback;

// Insert callback into url or form data
if ( jsonProp ) {
s[ jsonProp ] = s[ jsonProp ].replace( rjsonp, "$1" + callbackName );
} else if ( s.jsonp !== false ) {
s.url += ( ajax_rquery.test( s.url ) ? "&" : "?" ) + s.jsonp + "=" + callbackName;
}

// Use data converter to retrieve json after script execution
s.converters["script json"] = function() {
if ( !responseContainer ) {
jQuery.error( callbackName + " was not called" );
}
return responseContainer[ 0 ];
};

// force json dataType
s.dataTypes[ 0 ] = "json";

// Install callback
overwritten = window[ callbackName ];
window[ callbackName ] = function() {
responseContainer = arguments;
};

// Clean-up function (fires after converters)
jqXHR.always(function() {
// Restore preexisting value
window[ callbackName ] = overwritten;

// Save back as free
if ( s[ callbackName ] ) {
// make sure that re-using the options doesn't screw things around
s.jsonpCallback = originalSettings.jsonpCallback;

// save the callback name for future use
oldCallbacks.push( callbackName );
}

// Call if it was a function and we have a response
if ( responseContainer && jQuery.isFunction( overwritten ) ) {
overwritten( responseContainer[ 0 ] );
}

responseContainer = overwritten = undefined;
});

// Delegate to script
return "script";
}
});

[解决办法]
你可以试试这样
$('li.caption').live("hover",function () {
$(".cover", this).stop().animate({ top: '0px' }, { queue: false, duration: 160 });


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


或者你设置你的
$.ajax({
type: "get",
dataType: "html",
url: '/Ashx/act.ashx',
data: sql,
async:flase,//设置为同步请求,让这段ajax 执行完毕再在执行别的操作
success: function (data) {
$("#" + showul).html(data)
},
});

读书人网 >JavaScript

热点推荐