I have a 자바스크립트 함수를 지나갈 수 있는 매개변수입니다. &Prs. seabreeze 요소의 id (숨겨진 기입란) 내 웹 페이지. 이 요소의 값을 변경하십시오 싶다.
function myFunc(variable){
var s= document.getElementById(variable);
s.value = 'New value'
}
이 때문에 오류가 발생할 때, 나는 그 나는한다 객체인지 null 값을 설정할 수 없습니다. 하지만 내가 아는 I see it is not null 객체에는 때문에 html 코드에 의해 생성된 브라우저. 어쨌든 디버그로 다음 코드를 찾았다
function myFunc(variable){
var x = variable;
var y = 'This-is-the-real-id'
alert(x + ', ' + y)
var s= document.getElementById(x);
s.value = 'New value'
}
경고 메시지가 표시되며, 그래도 매개변수입니다 때 모두 동일한 내려받습니다 오류:. 그러나 나는한다 때 모든 것을 작동합니다
var s= document.getElementById('This-is-the-real-id');
s.value = 'New value'
어떻게 이 desktopplatforms 수리입니다.
편집
나는 어떤 요소의 설정하면 숨겨짐 필드와 id 는 det 무중단으로, 페이지가 로드되면. $ (document) 그레이디 나도 날잡아 추가되든지 이런 기능이 작동하지 않았다
미풍크 (변수) 전에 실행될 경우 extarea 렌더링됩니다 널 예외 오류 페이지가 얻을 수 있습니다.
<html>
<head>
<title>index</title>
<script type="text/javascript">
function myFunc(variable){
var s = document.getElementById(variable);
s.value = "new value";
}
myFunc("id1");
</script>
</head>
<body>
<textarea id="id1"></textarea>
</body>
</html>
//Error message: Cannot set property 'value' of null
따라서 해당 페이지에서 smartupdate extarea 가 있을 다음 미풍크 빈다우드로 로이드 또는 $ (document) 그레이디 함수 호출을 사용할 수 있습니다.
주어진
<div id="This-is-the-real-id"></div>
그럼
function setText(id,newvalue) {
var s= document.getElementById(id);
s.innerHTML = newvalue;
}
window.onload=function() { // or window.addEventListener("load",function() {
setText("This-is-the-real-id","Hello there");
}
그리웠댔지 됩니다
<input id="This-is-the-real-id" type="text" value="">
그럼
function setValue(id,newvalue) {
var s= document.getElementById(id);
s.value = newvalue;
}
window.onload=function() {
setValue("This-is-the-real-id","Hello there");
}
그리웠댔지 됩니다
<! - begin 스니핏: js 숨기십시오: 진정한 콘솔: 진정한 바벨. > 거짓값 -;
function setContent(id, newvalue) {
var s = document.getElementById(id);
if (s.tagName.toUpperCase()==="INPUT") s.value = newvalue;
else s.innerHTML = newvalue;
}
window.addEventListener("load", function() {
setContent("This-is-the-real-id-div", "Hello there");
setContent("This-is-the-real-id-input", "Hello there");
})
<div id="This-is-the-real-id-div"></div>
<input id="This-is-the-real-id-input" type="text" value="">
끝 - < 스니핏 >;!
<html>
<head>
<script>
function updateTextarea(element)
{
document.getElementById(element).innerText = document.getElementById("ment").value;
}
</script>
</head>
<body>
<input type="text" value="Enter your text here." id = "ment" style = " border: 1px solid grey; margin-bottom: 4px;"
onKeyUp="updateTextarea('myDiv')" />
<br>
<textarea id="myDiv" ></textarea>
</body>
</html>
그 아래 같은 시도하시겠습니까 작동합니다.
<html>
<head>
<script>
function displayResult(element)
{
document.getElementById(element).value = 'hi';
}
</script>
</head>
<body>
<textarea id="myTextarea" cols="20">
BYE
</textarea>
<br>
<button type="button" onclick="displayResult('myTextarea')">Change</button>
</body>
</html>
각 요소에 대한 유형, 특정 속성 값을 설정하여 사용할 수 있습니다. E. g.:
<div id="theValue1"></div>
window.document.getElementById("theValue1").innerText = "value div";
<input id="theValue2"></input>
window.document.getElementById("theValue2").value = "value input";
[여기서 예를 볼 수 있습니다!] [1]
[1]: https://codepen.io/anon/pen/rbVvvW = 1010년 편집자?