2011-05-04 2 views
1

이미지 제목에 아스키 문자 참조 (®)를 사용하는 데 문제가 있습니다. 그것은 html 본문을 통해 설정할 때 잘 작동하지만 javascript를 통해 동일한 작업을 수행하려고하면 작동하지 않습니다.js를 사용하여 img title 속성에 ascii 참조를 설정할 수없는 이유는 무엇입니까?

확인 sscce :

<style type="text/css">body {background-color:black;}</style> 
<script src="https://ajax.googleapis.com/ajax/libs/prototype/1.7.0.0/prototype.js"></script> 
<p>this image has the correct ascii character title:<br /><img src="http://www.prototypejs.org/images/logo-home.gif" id="img1" title="&reg;" /></p> 
<p>but why can't I set the same value via javascript?<br /><img src="http://www.prototypejs.org/images/logo-home.gif" id="img2" /></p> 
<script type="text/javascript"> 
$("img2").title = "&reg;"; 
</script> 

감사합니다.

+0

참조 http://stackoverflow.com/questions/5890175/is-it-not-possible-to-use-html-entities-in-a-title-attribute – simshaun

+0

아, 이해할 것 같습니다 .. 거기에 있습니다. 어디서나 'htmlToText'유틸리티 기능? html 요소를 프로그래밍 방식으로 작성하여 텍스트를 추출해야하는 것을 싫어합니다. – pstanton

답변

2

$ ("IMG2"). 제목 = "®", 작동합니다.

html 엔티티는 순수 텍스트로 번역되지 않았습니다.

1

http://paulschreiber.com/blog/2008/09/20/javascript-how-to-unescape-html-entities/

에 대한 답변의 풍부한있다 그러나 그것의 기본 취지는 노드가 아닌 HTML의 텍스트를 설정하는 것입니다, 그래서 HTML 엔티티가 없습니다.

그러나 숨겨진 개체의 innerHTML을 설정하고 해당 텍스트 값을 읽을 수 있습니다. 또는 소스 인코딩이 reg 심볼을 직접 입력 할 수 있다고 가정합니다.

$ ("IMG2"). 제목 = '\ u00AE'를 사용하지 않을 경우