jquery多版本兼容⽅法
框架使⽤的jq版本与上传⽂件的jq版本不⼀样
开发者技术变现资源聚集地
/*
* jQuery 1.2.1 - New Wave Javascript
是,Conflict()帮你解决了这个烦恼。
*/
<script  type="text/javascript"  src="<%=basePath%>/resources/Js/jquery.js" ></script>
<!--  <script type="text/javascript" src="<%=basePath%>/resources/Js/ajaxfileupload.js" > </script> -->
<script type="text/javascript" >
var $jq = Conflict(true);
$jq.extend({
createUploadIframe: function(id, uri)
{
//create frame
var frameId = 'jUploadFrame' + id;
if(window.ActiveXObject) {
if($jq.browser.version=="9.0" || $jq.browser.version=="10.0"){
var io = ateElement('iframe');
io.id = frameId;
io.name = frameId;
}else if($jq.browser.version=="6.0" || $jq.browser.version=="7.0" || $jq.browser.version=="8.0"){
var io = ateElement('<iframe id="' + frameId + '" name="' + frameId + '" />');
if(typeof uri== 'boolean'){
io.src = 'javascript:false';
}
else if(typeof uri== 'string'){
io.src = uri;
}
}
}
else {
var io = ateElement('iframe');
io.id = frameId;
io.name = frameId;
}
io.style.position = 'absolute';
p = '-1000px';
io.style.left = '-1000px';
document.body.appendChild(io);
return io
},
createUploadForm: function(id, fileElementId,data)
{
//create form
var formId = 'jUploadForm' + id;
var fileId = 'jUploadFile' + id;
var form = $jq('<form  action="" method="POST" name="' + formId + '" id="' + formId + '" enctype="multipart/form-data"></form>');
var oldElement = $jq('#' + fileElementId);
if(typeof(fileElementId) == 'string'){
fileElementId = [fileElementId];
}
for(var i in fileElementId){
var oldElement = $jq('#' + fileElementId[i]);
var newElement = $jq(oldElement).clone();
$jq(oldElement).attr('id', fileId);
$jq(oldElement).before(newElement);
$jq(oldElement).appendTo(form);
}
//set attributes
if (data) {
for ( var i in data) {
$jq('<input type="hidden" name="' + i + '" value="' + data[i] + '" />').appendTo(form);
}
}
$jq(form).css('position', 'absolute');
$jq(form).css('top', '-1200px');
$jq(form).css('left', '-1200px');
$jq(form).appendTo('body');jquery框架原理
return form;
},
ajaxFileUpload: function(s) {
// TODO introduce global settings, allowing the client to modify them for all requests, not only timeout
s = $jq.extend({}, $jq.ajaxSettings, s);
var id = new Date().getTime()
var form = $jq.createUploadForm(id, s.fileElementId,s.data);
var io = $jq.createUploadIframe(id, s.secureuri);
var frameId = 'jUploadFrame' + id;
var formId = 'jUploadForm' + id;
// Watch for a new set of requests
if ( s.global && ! $jq.active++ )
{
$igger( "ajaxStart" );
}
var requestDone = false;
// Create the request object
var xml = {}
if ( s.global )
$igger("ajaxSend", [xml, s]);
// Wait for a response to come back
var uploadCallback = function(isTimeout)
{
var io = ElementById(frameId);
try
{
tWindow)
{
}else tDocument)
{
}catch(e)
{
$jq.handleError(s, xml, null, e);
}
if ( xml || isTimeout == "timeout")
{
requestDone = true;
var status;
try {
status = isTimeout != "timeout" ? "success" : "error";
// Make sure that the request was successful or notmodified
if ( status != "error" )
{
// process the data (runs the xml through httpData regardless of callback)
var data = $jq.uploadHttpData( xml, s.dataType );
// If a local callback was specified, fire it and pass it the data
if ( s.success )
s.success( data, status );
// Fire the global callback
if( s.global )
$igger( "ajaxSuccess", [xml, s] );
} else
$jq.handleError(s, xml, status);
} catch(e)
{
status = "error";
$jq.handleError(s, xml, status, e);
}
// The request was completed
if( s.global )
$igger( "ajaxComplete", [xml, s] );
// Handle the global AJAX counter
if ( s.global && ! --$jq.active )
$igger( "ajaxStop" );
/
/ Process result
if ( splete )
splete(xml, status);
$jq(io).unbind()
setTimeout(function()
{    try
{
$jq(io).remove();
$jq(form).remove();
} catch(e)
{
$jq.handleError(s, xml, null, e);
}
}, 100)
xml = null
}
}
// Timeout checker
if ( s.timeout > 0 )
{
setTimeout(function(){
// Check to see if the request is still happening
if( !requestDone ) uploadCallback( "timeout" );
}, s.timeout);
}
try
{
// var io = $jq('#' + frameId);
var form = $jq('#' + formId);
$jq(form).attr('action', s.url);
$jq(form).attr('method', 'POST');
$jq(form).attr('target', frameId);
ding)
{
}
else
{
}
$jq(form).submit();
} catch(e)
{
$jq.handleError(s, xml, null, e);
}
if(window.attachEvent){
}
else{
return {abort: function () {}};
},
uploadHttpData: function( r, type ) {
var data = !type;
data = type == "xml" || data ? r.responseXML : r.responseText;
/
/ If the type is "script", eval it in global context
if ( type == "script" )
$jq.globalEval( data );
// Get the JavaScript object, if JSON is used.
if ( type == "json" ){
////////////以下为新增代码///////////////
data = r.responseText;
var start = data.indexOf(">");
if(start != -1) {
var end = data.indexOf("<", start + 1);
if(end != -1) {
data = data.substring(start + 1, end);
}
}
///////////以上为新增代码///////////////
eval( "data = " + data );
// evaluate scripts within html
}
if ( type == "html" )
$jq("<div>").html(data).evalScripts();
//alert($jq('param', data).each(function(){alert($jq(this).attr('value'));}));
return data;
},
handleError: function( s, xhr, status, e ) {
// If a local callback was specified, fire it
if ( s.error ) {
}
// Fire the global callback
if ( s.global ) {
(s.context ? $t) : $jq.event).trigger( "ajaxError", [xhr, s, e] );
}
}
})
function uploadFile() {
var file = $jq("#inputFile").val();
if (file != "" && file != null) {
$jq.ajaxFileUpload({
url: "localhost:8080/autoDrainageServlet/myQQNumberServlet",
secureuri: false,
//name:'inputFile',
fileElementId: 'inputFile',//file标签的id
dataType: 'json',//返回数据的类型
global:true,
data:{headCode:'SumAmount'},
complete:function(){
$jq.messager.progress('close');
$jq("#inputFile").val("");
},
success: function (data) {
if (data =="0") {
// alert("上传成功");
queryCodShipData();
} else {
alert("上传失败");
}
},
});
}
}
</script>
/*
jquery-1.8.1
*/
<script type="text/javascript" src="<%=basePath%>/resources/Js/jquery-1.8.1.min.js"></script>    <script type="text/javascript" src="<%=basePath%>/resources/Js/bui-min.js"></script>

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。