Javascript 不要跑,你让生活变得简单多了。例如,如果您是页面作者,或者能够将功能限制为仅处理单个文本节点中的单词,则上述操作有效。但是在节点(或更糟糕的元素)上拆分单词会让你非常痛苦。但这是可以做到的。我目前正在研究这件事——但感谢你的描述性回答,似乎要得到一个完整的
Javascript 不要跑,你让生活变得简单多了。例如,如果您是页面作者,或者能够将功能限制为仅处理单个文本节点中的单词,则上述操作有效。但是在节点(或更糟糕的元素)上拆分单词会让你非常痛苦。但这是可以做到的。我目前正在研究这件事——但感谢你的描述性回答,似乎要得到一个完整的,javascript,jquery,Javascript,Jquery,不要跑,你让生活变得简单多了。例如,如果您是页面作者,或者能够将功能限制为仅处理单个文本节点中的单词,则上述操作有效。但是在节点(或更糟糕的元素)上拆分单词会让你非常痛苦。但这是可以做到的。我目前正在研究这件事——但感谢你的描述性回答,似乎要得到一个完整的解决方案并不十分简单!也谢谢你提醒我忽略一些元素-永远不知道什么客户将进入CMS!javascript中的一个严重问题是试图编写在任何地方都能工作的通用函数。如果您限制了函数必须运行的上下文,则会使操作变得非常简单。例如,如果您是页面作者,或者
不要跑,你让生活变得简单多了。例如,如果您是页面作者,或者能够将功能限制为仅处理单个文本节点中的单词,则上述操作有效。但是在节点(或更糟糕的元素)上拆分单词会让你非常痛苦。但这是可以做到的。我目前正在研究这件事——但感谢你的描述性回答,似乎要得到一个完整的解决方案并不十分简单!也谢谢你提醒我忽略一些元素-永远不知道什么客户将进入CMS!javascript中的一个严重问题是试图编写在任何地方都能工作的通用函数。如果您限制了函数必须运行的上下文,则会使操作变得非常简单。例如,如果您是页面作者,或者能够将功能限制为仅处理单个文本节点中的单词,则上述操作有效。但是在节点(或更糟糕的元素)上拆分单词会让你非常痛苦。但这是可以做到的。
[{
terms: 'first term, second term',
youtubeid: '123qwerty789'
},{
terms: 'match, all, of these',
youtubeid: '123qwerty789'
},{
terms: 'only one term',
youtubeid: '123qwerty789'
},
etc]
<div id="my-wrapper">
<ol>
<li>This is some text here without a term</li>
<li>This is some text here with only one term</li>
<li>This is some text here that has <strong>the first term</strong> nested!</li>
</ol>
</div>
$('#my-wrapper').contents().each(function(){
// Unfortunately only provides the <ol> -
// How would I modify this to give me all nested elements in a loopable format?
});
$('#my-wrapper li')
var recurse = function(el) {
// if text node or comment node
if(el.nodeType == 3 || el.nodeType == 8) {
// do your work here
console.log("Text: " + el.nodeValue);
}else {
for(var i = 0, children = el.childNodes, len = children.length; i < len; i++) {
recurse(children[i]);
}
}
}
recurse(document.getElementById("my-wrapper"));
function processTextNodes(element) {
element = element || document.body;
var self = arguments.callee; // or processTextNodes
var el, els = element.childNodes;
for (var i=0, iLen=els.length; i<iLen; i++) {
el = els[i];
// Exclude script element content
// May need to add other node types here
if (el.nodeType == 1 && el.tagName && el.tagName.toLowerCase() != 'script') {
// Have an element node, so process it
self(el);
// Othewise see if it's a text node
// If working with XML, add nodeType 4 if you want to process
// text in CDATA nodes
} else if (el.nodeType == 3) {
/* do something with el.data */
}
}
/* return a value? */
}