이 쉽게이 짧은 jQuery를 명령을 수행 할 수 있습니다
$('p meta').remove();
쉽게 선택에 설명되어 <p>
의 자녀 <meta>
태그 찾기 및 remove()
한 번에 일치하는 모든 요소를 제거합니다.
당신의 HTML 문자열에 있지만 여전히 나쁘지 않다 때 좀 더 복잡하게
:
var content = '...insert above html here...';
// insert into a div to parse as HTML
var $content = $('<div />').html(content);
// find all <meta> tags inside <p> and remove them
$content.find('p meta').remove();
// convert contents back to a string
content = $content.html();
근무 바이올린 : http://jsfiddle.net/WVWPS/
위의 코드는 나를 위해 작동하지만,이 월 어떤 브라우저에서는 <meta>
이 문서 본문의 유효한 HTML 요소가 아니기 때문에 문제가 될 수 있습니다. This question은 jQuery가 어떤 경우에는 <meta>
과 같은 요소를 제대로 만들지 못한다고 제안합니다.
당신이 브라우저 별 문제가 발생하면,이 같은 시도 할 수 있습니다 :
var content = '...insert above html here...';
// convert <meta> tags to <span> tags with a "meta" class
content = content.replace(/<meta /g, '<span class="meta" ');
// insert into a div to parse as HTML
var $content = $('<div />').html(content);
// find all .meta elements inside <p> and remove them
$content.find('p .meta').remove();
// convert contents back to a string
content = $content.html();
작업 바이올린 : 우리는 HTML을 구문 분석하기 전에 http://jsfiddle.net/a4Wa3/
, 우리는 일을하는지에 문자열을-교체 대신 <meta>
태그를 <span class="meta">
태그로 변환하십시오.
이 마지막 옵션은 매우 취약한 솔루션이므로 메타 태그 생성 방법을 변경하면 문제가 발생할 수 있습니다. 일반적으로 정규식 또는 기타 문자열 함수로 HTML을 조작하는 것은 피해야합니다 (serious side effects을 가질 수 있음).
'정규식은 필수입니까? –
@badZoke, 아니요, JavaScript 또는 jQuery를 사용하여 상위 단락 태그로 메타를 대체하는 항목 –