use*_*613 10 javascript jquery jquery-mobile
我需要从字符串中删除span标记.它是否可行.?
我的意见是这个
In open <span class="">court</span> at 11:01 a.m.)
THE <span class="">COURT</span>: Our interpreter.
(NOTE: Spanish interpreter Rosa Lopez-Gaston sworn.)
SPEAKER 2: Judy Faviell for the State.
SPEAKER 1: Carey Bhalla for Mr. Garcia, who is present in custody, Your Honor.
SPEAKER 2: Judge, we do have a change of plea, which I will tender to the <span class="">Court</span>.
THE <span class="">COURT</span>:
Run Code Online (Sandbox Code Playgroud)
输出就是这个
In open court at 11:01 a.m.)
THE COURT: Our interpreter.
(NOTE: Spanish interpreter Rosa Lopez-Gaston sworn.)
SPEAKER 2: Judy Faviell for the State.
SPEAKER 1: Carey Bhalla for Mr. Garcia, who is present in custody, Your Honor.
SPEAKER 2: Judge, we do have a change of plea, which I will tender to the <span class="">Court</span>.
THE COURT:
Run Code Online (Sandbox Code Playgroud)
nnn*_*nnn 25
对于您编辑的问题,您可以这样做:
var str = // your string here
str = str.replace(/<\/?span[^>]*>/g,"");
Run Code Online (Sandbox Code Playgroud)
如果您需要转换的“字符串”已经是 HTML 元素内容的一部分,那么这里有一个 POV(普通旧版)JavaScript(即非 jQuery)解决方案:
element.outerHTML = element.innerHTML;
Run Code Online (Sandbox Code Playgroud)
因此,例如,如果您想解开/删除文档中的所有跨度,您可以循环遍历所有相关元素,如下所示:
document.querySelectorAll('span').forEach(spanElmt => {
spanElmt.outerHTML = spanElmt.innerHTML;
};
Run Code Online (Sandbox Code Playgroud)
以下示例演示了其工作原理,显示指定的元素被删除/展开,而嵌套在其中的任何元素保持不变。
element.outerHTML = element.innerHTML;
Run Code Online (Sandbox Code Playgroud)
document.querySelectorAll('span').forEach(spanElmt => {
spanElmt.outerHTML = spanElmt.innerHTML;
};
Run Code Online (Sandbox Code Playgroud)
const btn = document.querySelector('button');
const redSpan = document.querySelector('.red' );
const par = document.querySelector('p' );
const div = document.querySelector('div' );
const unwrapElmt = elmt => {
elmt.outerHTML = elmt.innerHTML; // *** the key line
};
const showParagraphHTML = () => {
div.textContent = 'HTML for the above line: ' + par.innerHTML;
};
showParagraphHTML();
const unwrapRedSpan = () => {
unwrapElmt(redSpan);
showParagraphHTML();
};
btn.addEventListener('click', unwrapRedSpan);Run Code Online (Sandbox Code Playgroud)
该解决方案要求要修改的文本/字符串已经是 HTML 文档或元素的一部分。因此,它不适用于如下所示的独立字符串:
const myStr = 'some <span><i>very</i> interesting</span> thing';
myStr.outerHTML = myStr.innerHTML; // won't work
Run Code Online (Sandbox Code Playgroud)
最终,这就是最初的问题所要问的。但是,上面的字符串可以成为临时中间孤立 HTML 容器元素的一部分,之后此解决方案就变得可用:
const myStr = 'some <span><i>very</i> interesting</span> thing';
const container = document.createElement('div'); // a temporary orphan div
container.innerHTML = myStr;
const spanToUnwrap = container.querySelector('span');
spanToUnwrap.outerHTML = spanToUnwrap.innerHTML;
const myModifiedStr = container.innerHTML;
Run Code Online (Sandbox Code Playgroud)
例如,如果您只想从单个字符串中删除一个或几个简单的标签(如问题中所建议的那样),这可能看起来有点矫枉过正。在这种情况下,基于正则表达式的替换可能就足够了,例如另一个答案中提出的替换。然而,对于任何更复杂的事情(例如,只想删除特定类的跨度等),使用临时孤立 HTML 元素作为中间元素可能非常有用。
请注意,本节中描述的策略最终是一个不同的答案,展示了如何使用 jQuery。但是,如果您的初始字符串已经是 HTML 元素的一部分,并且您还不需要 jQuery,那么当您真正需要的就是这个outerHTML/ swap 时,使用 jQuery 可能会有点过分。innerHTML
| 归档时间: |
|
| 查看次数: |
18118 次 |
| 最近记录: |