问问题
53 次
2 回答
1
如果你想正确地做到这一点(同时保留你的标记),它会变得有点复杂,因为你需要一个位于元素直接子.fraction
元素的文本节点中的斜线;并且 jQuery 不能很好地处理文本节点。所以:
$(function() {
$('.fraction').each(function(_, e) {
var offset = 0, html = '<span class="top">', found = false;
$.each(e.childNodes, function(_, c) {
if (c.nodeType == Node.TEXT_NODE) {
if (m = c.textContent.match(/(.*)\/(.*)/)) {
found = true;
html += m[1] + '</span>/<span class="bottom">' + m[2];
} else {
html += c.textContent;
}
} else {
html += c.outerHTML;
};
});
if (found) {
e.innerHTML = html + '</span>';
}
});
});
.top {
color: red;
}
.bottom {
color: blue;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span class="fraction"><strike>1</strike>/2</span>
<span class="fraction">2/2</span>
于 2016-04-28T01:45:59.150 回答