我有这个简单的js:
var innerText = document.getElementById("TaskId").options[0].text;
我想知道如何将其转换为 jQuery:
var innerS = $("#TaskId");
var innerText = innerS.options[0].text;
这会引发错误:
innerS.options is undefined
编辑
根据一些辩论,我一起进行了快速速度测试:
js:
var startDat = Date.now();
for (var c = 0; c < 100000; c++) {
var com = $("#TaskId").get(0);
}
var endDat = Date.now();
alert("jQ.get(0) took " + (endDat - startDat) + "ms");
var startD = Date.now();
for (var co = 0; co < 100000; co++) {
var com = $("#TaskId")[0];
}
var endD = Date.now();
alert("jQ[0] took " + (endD - startD) + "ms");
var startDa = Date.now();
for (var comp = 0; comp < 100000; comp++) {
var compa = document.getElementById("TaskId");
}
var endDa = Date.now();
alert("js took " + (endDa - startDa) + "ms");
结果:
jQ.get(0) took 1042ms
jQ[0] took 1057ms
js took 136ms