@zizou10235

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

htmlタグに囲まれていない文字列の取得 jquery

解決したいこと

htmlで、タグ内ではない文字の取得をjqueryでする場合どのように書けばよろしいでしょうか?

konnnitiha

helloworld ← これを取得したい。

文字列を取得して、消去したいです。
どなたか詳しい方いらしたら回答お願い致します。

0 likes

1Answer

HTMLは<html>から始まって</html>で終わる文書なのでタグ内ではない文字というものは存在しません。

jQueryはHTML要素を操作するのには向いていますが、要素でない通常の文字列を置換するならばJavaScriptの標準の関数を使用することになるかと思います。(下のどちらもhelloworldを空文字列に置換する方法)

// jQueryを使用する場合
const $parent = $('親要素セレクタ');
$parent.html($parent.html().replace('helloworld', ''));

// jQueryを使用しない場合(JavaScript標準の関数のみ使用)
const parent = document.querySelector('親要素セレクタ');
parent.innerHTML = parent.innerHTML.replace('helloworld', '');
1Like

Comments

  1. @zizou10235

    Questioner

    回答いただきありがとうございます。
  2. 解決できたでしょうか? 解決できたのであれば質問のクローズをお願いします。解決できていなければ回答を試してみてどうだったか教えていただけると助かります。

Your answer might help someone💌