Created
November 1, 2010 12:05
-
-
Save think49/658057 to your computer and use it in GitHub Desktop.
ExtXPathEvaluator.js : DOM L3 XPath の XPathEvaluator を拡張してノード操作を簡単にしたJavaScriptライブラリ
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ExtXPathEvaluator.js | |
function ExtXPathEvaluator () { | |
if (!(this instanceof ExtXPathEvaluator)) { | |
throw new Error(this + ' is not a object created by constructor'); | |
} | |
return this; | |
} | |
(function () { | |
this.evaluate = function (domObject, expression /*, contextNode*/) { | |
var contextNode; | |
contextNode = arguments[2]; | |
if (!contextNode) { | |
contextNode = domObject; | |
} | |
this.xPathResult = domObject.evaluate(expression, contextNode, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null); | |
return this; | |
}; | |
this.replace = function (property, searchValue, replaceValue) { | |
var xPathResult, node, i; | |
xPathResult = this.xPathResult; | |
for (i = xPathResult.snapshotLength - 1; i > -1; i--) { | |
node = xPathResult.snapshotItem(i); | |
node[property] = node[property].replace(searchValue, replaceValue); | |
} | |
return this; | |
}; | |
this.forEach = function (callbackfn /*, thisArg*/) { | |
var xPathResult, thisArg, k, len, foo; | |
if (typeof callbackfn !== 'function') { | |
throw new TypeError(callbackfn + ' is not a function'); | |
} | |
xPathResult = this.xPathResult; | |
if (arguments.length < 2) { | |
for (k = 0, len = xPathResult.snapshotLength; k < len; k++){ | |
callbackfn(xPathResult.snapshotItem(k)); | |
} | |
} else { | |
thisArg = arguments[1]; | |
for (k = 0, len = xPathResult.snapshotLength; k < len; k++){ | |
callbackfn.call(thisArg, xPathResult.snapshotItem(k)); | |
} | |
} | |
return this; | |
}; | |
}).call(ExtXPathEvaluator.prototype); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
下記URLで解説しています。
ExtXPathEvaluator.js
http://vird2002.s8.xrea.com/javascript/ExtXPathEvaluator.html
このライブラリは「DOM L3 XPath」のメソッドを利用しています。
未対応ブラウザでも XPath を利用する出来るようにするためには「JavaScript-XPath」を併用してください。
JavaScript-XPath – CodeRepos::Share – Trac
http://coderepos.org/share/wiki/JavaScript-XPath
OKWave で回答しました。
javascriptで、<body>タグ内のnodeNameが"#te | OKWave
http://okwave.jp/qa/q6283012.html
google翻訳apiで、#TEXTを翻訳する。 | OKWave
http://okwave.jp/qa/q6284674.html