Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript JS到jQuery的完整版本_Javascript_Jquery - Fatal编程技术网

Javascript JS到jQuery的完整版本

Javascript JS到jQuery的完整版本,javascript,jquery,Javascript,Jquery,我有这个: function createObject() { var request_type; var browser = navigator.appName; if(browser == "Microsoft Internet Explorer"){ request_type = new ActiveXObject("Microsoft.XMLHTTP"); } else { request_type = new XMLHttpRequest();

我有这个:

function createObject() {  
  var request_type;
  var browser = navigator.appName; 
  if(browser == "Microsoft Internet Explorer"){
    request_type = new ActiveXObject("Microsoft.XMLHTTP");
  } else {
    request_type = new XMLHttpRequest();
  }
  return request_type;
}

var http = createObject();
var nocache = 0;

function insert() {
  document.getElementById('insert_response').innerHTML = "To Sek .. "
  var bID= encodeURI(document.getElementById('bID').value);
  var kommentar= encodeURI(document.getElementById('kommentar').value);
  nocache = Math.random();
  http.open('get', 'insert.php?bID='+bID+'&kommentar=' +kommentar+'&nocache = '+nocache);
  http.onreadystatechange = insertReply;
  http.send(null);
}

function insertReply() {
  if(http.readyState == 4){
  var response = http.responseText;  
  document.getElementById('insert_response').innerHTML = response;
  if ($("#box[value=1]").length > 0) {  window.parent.showMessage("Video Is OK"); }
}
}
我想缩短代码,让它充分使用jQuery。例如,我听说过连载;而不是使用http.open等,但是在这种情况下我应该如何使用它呢


我真的需要createobject中的所有内容来创建http吗?

大部分内容都可以通过调用来清理

首先,它将为您提取特定于浏览器的详细信息,因此您不必检查如何获取XMLHttpRequest

然后可以使用jQuery通过选择器获取元素。要按id选择任何元素,请使用以下语法:

$("#<id>")
hashtag表示要选择具有指定id的元素。从那里,您可以使用一些函数来获取特定元素内部的值


最后,您可以使用创建一个函数并将其作为回调传递给get函数,该函数在调用完成时执行。然后,当调用完成时,您将使用相同的选择器常规属性函数来更改客户端上的值。

未测试,但我确信这正是您需要的:

function onInsertComplete(data,textstatus){
    $("#insert_response").html(data);
}

function doInsert(){
    $("#insert_response").html("To Sek...");
    var nocache = '0';
    var data = { bID : $("#bID").val(), kommentar: $("#kommentar").val(), nocache: nocache };
    $.get('insert.php', data, onInsertComplete);
    if ($("#box[value=1]").length > 0) {  
        window.parent.showMessage("Video Is OK"); 
    }
}

有什么事吗


这个效果很好!。。但它不执行window.parent.showmessage?所有这些都应该在POST中使用$.POST方法重新执行。您没有使用post,因为它在自己编写时有点复杂。现在使用jquery很简单,应该使用它
$('#result').load('ajax/test.html', function() {
  alert('Load was performed.');
});