내 페이지에서 문장을 찾고 클래스와 함께 span 태그로 감쌀 필요가 있습니다.자바 스크립트로 문장 주위에 감싸기 태그
<p><span class="mySpan">Welcome to the homepage</span> of the site. Enjoy your stay</p>
내 페이지에서 문장을 찾고 클래스와 함께 span 태그로 감쌀 필요가 있습니다.자바 스크립트로 문장 주위에 감싸기 태그
<p><span class="mySpan">Welcome to the homepage</span> of the site. Enjoy your stay</p>
이것은 당신이해야 할 것입니다 : 자바 스크립트를 사용
<body>
<h1>Home Page</h1>
<p>Welcome to the homepage of the site. Enjoy your stay</p>
</body>
, 나는에 P 컨텐츠를 변환하고 싶습니다 :
이
내 페이지입니다.innerHtml 또는 jQuery.html()를 사용하여 데이터를 기록 의 데이터를 분석 그렇게하는 간단한 예입니다 . 여기
var html = $("p").html();
html = "<span>" + html;
html = html.replace("homepage","homepage</span>");
$("p").html(html);
가 작동 jsFiddle입니다 :
<script>
P = document.getElementsByTagName('p');
search = 'Welcome to the homepage';
s = P[0].innerHTML.replace(search,'<span class="mySpan">' + search +'</span>');
P[0].innerHTML =s;
</script> <!-- always locate script at the end of the body -->
</body>
을하지만, 그것을 강화하기 위해, 당신은 ID를 포함 고려해야합니다 : 당신이 jQuery를 사용하지 않는 것을 선호하는 경우 http://jsfiddle.net/QjDvR/2/
이 시도 속성을 요소에 추가하고 document.getElementById ('the_id') 메소드로이 요소를 선택하십시오.
html 요소를 P에서 SPAN으로 변경하거나 P 요소의 스타일을 지정하려는 것은 확실하지 않습니다. – jgpATs2w
@ jgpATs2w 기본적으로 이것은 내가 찾고있는 결과입니다 : '
홈페이지에 오신 것을 환영합니다. 숙박을 즐기십시오
' – user1339316@Alex K. : jquery로 태그가 지정되지 않았기 때문에 답변이 다를 수 있습니다. – jgpATs2w