나는 자바 스크립트의 document.write를 메소드에 다음 코드를 사용하고 있습니다. 나를 안내하십시오.속성이있는 태그에 대해 document.write에 사용할 올바른 구문은 무엇입니까?</p> <pre><code><script type="text/javascript"> document.write("<font color="blue">["+time()+"]</font>"); </script> </code></pre> <p>을하지만이 작동하지 않는 것 :
답변
문제는 HTML과 직접적으로 관련이 없습니다. JavaScript에서 문자열 리터럴이 작동하는 방식 (그리고 거의 모든 다른 프로그래밍 언어)과 관련이 있습니다. "
문자로 구분 된 JavaScript 문자열 내에 "
문자를 사용하려면이 문자를 이스케이프 처리해야합니다 (\"
). 또는 '
을 사용하여 문자열 또는 속성 값을 구분할 수 있습니다.
document.write("<font color=\"blue\">["+time()+"]</font>");
document.write('<font color="blue">['+time()+"]</font>");
document.write("<font color='blue'>["+time()+"]</font>");
일반적으로 좋은 생각이 아니다 document.write
를 사용하여 HTML을 생성 말했다 (그것은 단지로드시 작동, 일반적으로 더 나은 더 안정적인 서버 측 코드 이상의 재사용 DOM 조작으로 대체)) 및 font
요소는 더 이상 사용되지 않습니다.
+1에 대한 명확한 설명 – 11684
var color = 'blue';
document.write("<font color="+color+">["+time()+"]</font>");
또는
document.write("<font color='blue'>["+time()+"]</font>");
누군가 내 대답을 싫어했습니다. 왜 설명해 주시겠습니까? – Oyeme
<script type="text/javascript">
document.write("<font color=\"blue\">[" + time() + "]</font>");
</script>
또는
<script type="text/javascript">
document.write('<font color="blue">[' + time() + ']</font>');
</script>
time
은 당신이 얻지 못한 위의 함수를 만들어 가정 자바 스크립트 - 난의 함수로 존재하지 않는 것으로 도시 코드 스 니펫에 포함되지 않았습니다. 또한 실험을하지 않는 한 실제로는 document.write
을 사용해서는 안됩니다. Firefox의 Firebug 또는 Safari/Chrome 웹 검사기에서 console.log
을 사용하여이 모든 것을 테스트 할 수 있습니다. 전체 페이지 새로 고침보다 실험이 빠릅니다. 또한 +
부호의 양쪽에 공백을 어떻게 추가했는지 알 수 있습니다. 쉽게 읽을 수 있습니다.
'document.write'의 올바른 구문은 사용하지 않는 것입니다. http://stackoverflow.com/questions/802854/why-is-document-write-considered-a-bad-practice – KooiInc