Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 文本索引到Html索引_Javascript_Html_Jquery - Fatal编程技术网

Javascript 文本索引到Html索引

Javascript 文本索引到Html索引,javascript,html,jquery,Javascript,Html,Jquery,我想问一下,是否有一种快速的方法可以从纯文本索引中找到html元素中单词的索引? 假设我有以下几点: innerHTML = <span>Hello</span><br><span>World!</span> innerText = Hello \n World! innerHTML=你好世界! innerText=你好\n世界! 假设我想在innerHTML中找到单词“World”的索引,我只知道它在innerText中的开始和结

我想问一下,是否有一种快速的方法可以从纯文本索引中找到html元素中单词的索引? 假设我有以下几点:

innerHTML = <span>Hello</span><br><span>World!</span>
innerText = Hello \n World!
innerHTML=你好
世界! innerText=你好\n世界!
假设我想在innerHTML中找到单词“World”的索引,我只知道它在innerText中的开始和结束索引

编辑


实际上,我试图替换am html文本中的一些错误单词,但保留它们的样式和结构,例如,如果一个单词位于span元素中,我想在不影响任何其他内容的情况下更改它

你所说的“它的纯文本索引”是什么意思?只是它在内部文本中的索引,例如“world”的索引这听起来像是一个X/Y问题,你问的是你自己的问题解决方案,而不是问题本身。你想解决的更大的问题是什么?如果你只替换错误的单词,我认为这不会影响字符串中的html,因为它不会改变索引,也不会被替换,除非你想将单词span改为xyz或br改为xyzy,你可以简单地
。替换('errowword','errowword'))
并重新分配
innerHTML
,如果需要全局替换,请使用正则表达式。