如何将一个HTML段落元素的文本值传递给另一个HTML段落元素

how to pass the text value of one html paragraph element to another html paragraph element using javascript in asp.net

本文关键字:元素 段落 HTML 文本 值传 另一个 一个      更新时间:2023-09-26

当调用javascript函数时,我试图将我的html段落元素之一的文本值传递给另一个html段落元素。当这个函数被调用时,它将加载具有放大图像和段落内容的div元素,这就是为什么我从一个html段落元素文本值传递到另一个段落元素文本值的原因。下面是我的代码供您参考:

Javacript:

<script type="text/javascript">
 function LoadDiv(url)
 {
    var img = new Image();
    var bcgDiv = document.getElementById("divBackground");
    var imgDiv = document.getElementById("divImage");
    var imgFull = document.getElementById("imgFull");
        img.onload = function() {
        imgFull.src = img.src;
        imgFull.style.display = "block";
        imgLoader.style.display = "none";
    };
    img.src= url;
    var width = document.body.clientWidth;
    if (document.body.clientHeight > document.body.scrollHeight)
    {
        bcgDiv.style.height = document.body.clientHeight + "px";
    }
    else
    {
        bcgDiv.style.height = document.body.scrollHeight + "px" ;
    }
    imgDiv.style.left = (width-650)/2 + "px";
    imgDiv.style.top =  "20px";
    bcgDiv.style.width="100%";
    bcgDiv.style.display="block";
    imgDiv.style.display="block";
     var artcontent = document.getElementById("TextBox2").value;
     document.getElementById("content").value = artcontent;
    /* after I added these two lines of codes below which I thought these could pass the        value but it just ruined my function and not loading anymore the div element I'm calling,it will now only flash the div element for 1 sec without event displaying the content of the artxt2 html paragraph.*/
     var artcon = document.getElementById('artxt1').innerHTML;
     document.getElementById('artxt2').innerHTML = artcon;
    return false;
 }
</script>

Asp。Net源代码:

<div id="divBackground" class="modal">
</div>
<div id="divImage">
    <table style="height: 100%; width: 100%">
        <tr>
            <td valign="middle" align="center">
                <img id="imgFull" alt="" src=""
                 style="display: none;
                height: 500px;width: 590px" />
            </td>
        </tr>
        <tr>
        <td >
        <p id ="artxt2" runat ="server" ></p>
        <textarea name = "artcont" id = "content" cols ="0" rows ="0" readonly ="readonly" style ="width :590px; height :100px; color :Blue; font-family :Verdana; display :block;"></textarea>  
        </td>
        </tr>
        <tr>
            <td align="center" valign="bottom">
                <input id="btnClose" type="button" value="close"
                 onclick="HideDiv()"/>
            </td>
        </tr>
    </table>
</div>

我需要有人能帮助我,找出我在这里错过了什么提前感谢。

用id更改名称

 artxt1 must be an id of a control not name.
 var artcon = document.getElementById('artxt1').innerHTML;
 document.getElementById('content').innerHTML = artcon;

试试这个-

var artcon = document.getElementById('<%=artxt1.ClientID%>').innerHTML;
document.getElementById('<%=artxt2.ClientID%>').innerHTML = artcon;

当你在控件上设置runat=server时,ID是由。net自动生成的(除非你特别告诉它使用静态ID,我认为这只在ASP中引入)。. NET 4.0)

所以您需要使用ClientID属性来获取控件http://msdn.microsoft.com/en-us/library/system.web.ui.control.clientid.aspx

生成的ID