我正在處理一些需要解析大量包含HTML片段的文件的代碼。看來jQuery對此非常有用,但是當我嘗試將jQuery加載到WScript或CScript之類的東西時,由於jQuery對窗口對象的許多引用,它會引發錯誤。如何在Windows Script Host中使用jQuery?
在沒有瀏覽器的情況下運行的代碼中使用jQuery有什麼實用的方法?
更新:回覆評論時,我已成功編寫使用new ActiveXObject('Scripting.FileSystemObject');
來閱讀文件內容的JavaScript代碼。我知道ActiveX是邪惡的,但這只是一個內部項目,用於從包含HTML片段的某些文件中將一些數據導出到適當的數據庫中。
另一個更新:到目前爲止我的代碼看起來大約是這樣的:
var fileIo, here;
fileIo = new ActiveXObject('Scripting.FileSystemObject');
here = unescape(fileIo.GetParentFolderName(WScript.ScriptFullName) + "\\");
(function() {
var files, thisFile, thisFileName, thisFileText;
for (files = new Enumerator(fileIo.GetFolder(here).files); !files.atEnd(); files.moveNext()) {
thisFileName = files.item().Name;
thisFile = fileIo.OpenTextFile(here + thisFileName);
thisFileText = thisFile.ReadAll();
// I want to do something like this:
s = $(thisFileText).find('input#txtFoo').val();
}
})();
更新:我張貼以及jQuery開發論壇這樣一個問題:http://forum.jquery.com/topic/how-to-use-jquery-without-a-browser#14737000003719577
http://stackoverflow.com/questions/2941411/executing-javascript-without-a-browser – pistache
工作錯誤的工具,說我。如果你進入jQuery選擇器檢查http://code.google .com/p/phpquery /,http://pypi.python.org/pypi/pyquery和其他。 – soulseekah
如果你使用Node,你可以使用Underscore.js,如果不是你所有的需求,它應該適合大多數。 –