テキスト ブロック内でタグの位置を取得することは可能ですか。たとえば、私は巨大な p タグを持っていて、その中にたくさんのテキストがあります。ユーザーは一連の span タグを p タグに動的に挿入するツールを使用できます。ある時点でユーザーは終了し、ユーザーが行ったことを保存したいと思います。制限により、p タグの内容全体を保存することはできず、代わりに個々のスパンを取得する必要があります。
初期テキスト
<p>Sam wanted a dog.
"If you're a good boy," said his father.
"When you can take care of it yourself" said his mother.
Sam cleaned up his room. He ate carrots and broccoli. He stopped making monster noises
at night to scare Molly, his older sister. He hung up his cap after baseball practice.
</p>
ユーザーの操作後
<p>Sam wanted a dog.
"If you're <span>a good boy,"</span> said his father.
"When you can take care of it yourself" said his mother.
Sam cleaned up his <span>room. He ate</span> carrots and broccoli. He stopped making monster noises
at night to scare Molly, his older sister. He hung up his cap after baseball practice.
</p>
私が探しているのは、スパンの開始位置と終了位置の範囲だと思います。これまでにできたのは、コンテンツをループすることだけですが、そこからどこへ行くべきかを見つけるのに行き詰まっています. 保存する必要がある理由は、ユーザーがコンテンツを残したままに戻ることを期待しているためです。そのため、ソリューションでは、span タグを元の場所に戻すことを検討する必要があります。
開始方法のサンプル JS
$("p").each(function (index) {
$(this).find("span").each(function () {
console.log(this);
});
});
私の実際の環境はもっと複雑ですが、解決策を絞り込むために基本に単純化しました。ヘルプ/提案は大歓迎です。