引用特定的DOM元素(例如< mark>),我们如何获得包含该元素的完整单词?
例如 :
H<mark>ell</mark>o Wor<mark>l</mark>d,and He<mark>llo</mark>,<mark>Pluto</mark>!
我希望得到以下输出:
>首先< mark>:你好
>第二:世界
>第三名:您好
>第四名:冥王星
var $marks = $("mark"); var tests = [ "Hello","World","Hello","Pluto",]; function getFullWord($elm) { // TODO: How can I do this? // This is obvIoUsly wrong. return $elm.html(); } var $marks = $("mark"); tests.forEach(function(c,i) { var word = getFullWord($marks.eq(i)); if (word !== c) { alert("Wrong result for index " + i + ". Expected: '" + c + "' but got '" + word + "'"); } });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> H<mark>ell</mark>o Wor<mark>l</mark>d,<mark>Pluto</mark>!