javascript的一些总结(8)

[b]16.通用的ajax库[/b]
var ajaxreq=false, ajaxCallback;
// ajaxRequest: Sets up a request
function ajaxRequest(filename) {
try {
// Firefox / IE7 / Others
ajaxreq= new XMLHttpRequest();
} catch (error) {
try {
// IE 5 / IE 6
ajaxreq = new ActiveXObject("Microsoft.XMLHTTP");
} catch (error) {
return false;
}
}
ajaxreq.open("GET",filename);
ajaxreq.onreadystatechange = ajaxResponse;
ajaxreq.send(null);
}
// ajaxResponse: Waits for response and calls a function
function ajaxResponse() {
if (ajaxreq.readyState !=4) return;
if (ajaxreq.status==200) {
// if the request succeeded...
if (ajaxCallback) ajaxCallback();
} else alert("Request failed: " + ajaxreq.statusText);
return true;
}

ajax的小例子
<html>
<head><title>Ajax Test</title>
<script language="JavaScript" type="text/javascript"
src="ajax.js">
</script>
</head>
<body>
<h1>Ajax Quiz Example</h1>
<form>
<p><b>Question:</b>
<span id="question">...
</span>
</p>
<p><b>Answer:</b>
<input type="text" name="answer" id="answer">
<input type="button" value="Submit" id="submit">
</p>
<input type="button" value="Start the Quiz" id="startq">
</form>
<script language="JavaScript" type="text/javascript"
src="quiz.js">
</script>
</body>
</html>


<?xml version="1.0" ?>
<questions>
<q>What DOM object contains URL information for the window?</q>
<a>location</a>
<q>Which method of the document object finds the object for an element?</q>
<a>getElementById</a>
<q>If you declare a variable outside a function, is it global or local?</q>
<a>global</a>
<q>What is the formal standard for the JavaScript language called?</q>
<a>ECMAScript</a>
</questions>


var qn=0;
// load the questions from the XML file
function getQuestions() {
obj=document.getElementById("question");
obj.firstChild.nodeValue="(please wait)";
ajaxCallback = nextQuestion;
ajaxRequest("questions.xml");
}
// display the next question
function nextQuestion() {
questions = ajaxreq.responseXML.getElementsByTagName("q");
obj=document.getElementById("question");
if (qn < questions.length) {
q = questions[qn].firstChild.nodeValue;
obj.firstChild.nodeValue=q;
} else {
obj.firstChild.nodeValue="(no more questions)";
}
}
// check the user's answer
function checkAnswer() {
answers = ajaxreq.responseXML.getElementsByTagName("a");
a = answers[qn].firstChild.nodeValue;
answerfield = document.getElementById("answer");
if (a == answerfield.value) {
alert("Correct!");
}
else {
alert("Incorrect. The correct answer is: " + a);
}
qn = qn + 1;
answerfield.value="";
nextQuestion();
}
// Set up the event handlers for the buttons
obj=document.getElementById("startq");
obj.onclick=getQuestions;
ans=document.getElementById("submit");
ans.onclick=checkAnswer;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
new 运算符在 JavaScript 中用于实例化一个类并创建一个对象。尽管 JavaScript 中的所有东西都是对象,但通过 new 运算符来创建对象有其特殊的作用。使用 new 运算符后,JavaScript 引擎会进行一系列的步骤来完成对象的创建和初始化。 具体来说,使用 new 运算符时,JavaScript 引擎会执行以下步骤: 1. 创建一个新的空对象。 2. 将新对象的原型指向类的原型对象,以便实现继承。 3. 将类的构造函数作为方法来调用,并将新对象作为构造函数的上下文(即 this)。 4. 如果构造函数返回一个对象,则返回该对象;否则,返回新创建的对象。 简言之,new 运算符通过使用类的构造函数来创建一个新对象,并且确保该对象具有正确的原型链和上下文。 引用中提到了 new 运算符的作用和意义,它允许我们在 JavaScript 中创建一个对象,这样我们可以根据类的定义来实例化对象。引用中给出了一个伪代码示例,展示了 JavaScript 引擎在执行 new 运算符时的工作流程。引用进一步建议了如何加深对 new 运算符的理解。 总结起来,JavaScript 中的 new 运算符用于实例化一个类并创建一个对象,它通过一系列步骤来完成对象的创建和初始化,确保对象具有正确的原型链和上下文。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [JS核心系列:理解 new 的运行机制](https://blog.csdn.net/gongzhuxiaoxin/article/details/52625842)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值