html中的javascript

html中的javascript,javascript,html,Javascript,Html,我正在使用javascript在运行时更改div标记的文本 怎样才能做到这一点 我的div标签如下: <div id="topdiv" style="color:Blue" onmouseover="button1();"> <input type="button" id="btndiv" onclick="edit1();"/> Div Tag </div> Div标签 我让用户在运行时在div中输入文本,这些文本应该显示在

我正在使用javascript在运行时更改div标记的文本

怎样才能做到这一点

我的div标签如下:

<div id="topdiv" style="color:Blue" onmouseover="button1();">
    <input type="button" id="btndiv"  onclick="edit1();"/>
     Div Tag
    </div>

Div标签
我让用户在运行时在div中输入文本,这些文本应该显示在div中。
有人能帮我吗?

应该是
innerHTML
innerHTM
不是javascript函数。

尝试document.getElementById('topdiv')。innerHTML=“Hello”

并通过正确的错误处理:

function edit1() {
    alert('you are in edit1');
    var topDiv = document.getElementById('topdiv');
    if (topDiv != null) {
        topDiv.innerHTML = 'hello';
    } else {
        alert('topdiv is nowhere to be found in this DOM');
    }
}
  • 仅仅通过一个id为的元素并不能得到神奇的变量。
    var something=document.getElementById('some-id')
  • 该属性称为
    innerHTML
    not
    innerHTM
  • innerHTML
    是字符串变量而不是函数。用
    =
    为它赋值,不要试图用
    ()
    调用它

  • 这应该通过指定id来实现。要获取
    div
    ,您应该使用
    document.getElementById('topdiv')
    。确实有一个WebKit特性,即带有ID的元素会自动扩展为全局变量,但这很值得怀疑,它是否会成为主流

    然后,
    innerHTM
    应为
    innerHTML
    ,您可以直接分配:

    foo.innerHTML = "hi there"
    
    你应该使用


    document.getElementById('topdiv')。innerHTML='hello'

    您应该使用引用而不是ID,使用
    this

    在这种情况下,
    表示触发事件的节点

    <div style="color:Blue" onmouseover="button1(this);">
        <input type="button" onclick="edit1(this);"/>
        Div Tag
    </div>
    
    function button1(divRef){
      //divRef is the reference to the DIV
    }
    function edit1(inputRef){
      //inputRef is the reference of the INPUT
      //inputRef.parentNode is the reference to the DIV
    }
    
    
    Div标签
    功能按钮1(divRef){
    //divRef是对DIV的引用
    }
    函数edit1(inputRef){
    //inputRef是输入的引用
    //inputRef.parentNode是对DIV的引用
    }
    
    在标准JavaScript使用中,您可以按照@DarinDimitrov的回答进行操作

    document.getElementById("topdiv").innerHTML = ('hello');
    
    一旦您对JavaScript感到满意,我建议您看看JQuery库——强大的语法可以让您编写简短、简洁的代码,如下所示:

    $("#topdiv").html('hello');
    
    你的档案

    <div id="topdiv" style="color:Blue" onmouseover="button1();"> Div Tag</div>
    <form><input type="button" id="btndiv" value="Edit" onClick="window.open('t2.html','popuppage','width=850,toolbar=1,resizable=1,scrollbars=yes,height=700,top=100,left=100');" value="Open popup"/></form>
    
    Div标签
    
    t2.html文件

    function sendValue (s){var selvalue = s.value;window.opener.document.getElementById('topdiv').innerHTML = selvalue;window.close();}
    
    <form name="selectform"><input name="details" value=""><input type=button  value="Copy input to parent opener" onClick="sendValue(this.form.details);"></form>
    
    函数sendValue{var selvalue=s.value;window.opener.document.getElementById('topdiv')。innerHTML=selvalue;window.close();}
    

    从这个来源得到的想法

    不,不是那样的。正如@gnur所说,innerHTML不是一个Javascript函数,而是一个必须分配给它的
    属性。这意味着,当我点击按钮时,会弹出一个光标,并在运行时输入文本。如果卡住了,也要这样做。现在我想在运行时进行更改。意思是当我点击按钮时,会弹出一个光标,并在运行时输入文本。@kawade,该文本应在何处输入?dimitrov:当我点击按钮时,光标应在div文本上闪烁,当用户编辑文本时,它应显示在html页面上。现在我想在运行时进行更改。这意味着当我点击按钮时,会弹出一个光标,并在运行时输入文本。如果卡住了,也要这样做。@kawade:这是一个新问题吗?如果你把这个评论换成一个新问题,你可能会得到更快的答案。
    
    <div id="topdiv" style="color:Blue" onmouseover="button1();"> Div Tag</div>
    <form><input type="button" id="btndiv" value="Edit" onClick="window.open('t2.html','popuppage','width=850,toolbar=1,resizable=1,scrollbars=yes,height=700,top=100,left=100');" value="Open popup"/></form>
    
    function sendValue (s){var selvalue = s.value;window.opener.document.getElementById('topdiv').innerHTML = selvalue;window.close();}
    
    <form name="selectform"><input name="details" value=""><input type=button  value="Copy input to parent opener" onClick="sendValue(this.form.details);"></form>