dpqjvoq9033 2014-04-05 17:07
浏览 6
已采纳

来自div的Javascript拷贝和符号输入故障

I cannot find an answer to this question...

The problem. Javascript copying text from DIV to input (textbox), fails to copy & as &, and copies it as &. See code plus example.

I have the following code (on different pages, just to simplify I put all here):

INPUT where I want the text copied.

<input name="artist" id="txtArtist" type="text" placeholder="Introduce the artist" />

DIV where the text is copied from. This data is populated from a SQL database.

<div id="art0"><div onclick="copyText(0)">Mumford & Sons</div></div>'

The function that fails...

function copyText(rowID){
    var content = document.getElementById('art'+rowID).innerHTML.replace(/<\/?[^>]+(>|$)/g, "
");
    document.getElementById("txtArtist").value = document.getElementById('art'+rowID).innerHTML;
    document.getElementById("txtArtist").value = content;
}

EXPECTED result inside textbox: Mumford & Sons

ACTUAL result inside textbox: Mumford &amp; Sons

Any help please?

Thank you all!!

  • 写回答

1条回答 默认 最新

  • doujionggan9570 2014-04-05 17:12
    关注

    Use .textContent instead of .innerHTML:

    var content = document.getElementById('art'+rowID).textContent;
    

    http://jsfiddle.net/4Tr5T/1/

    Note that .textContent is not supported by IE 8 and lower, so if you are so unfortunate, test for and use .innerText instead.

    var prop = 'textContent' in document.body ? 'textContent' : 'innerText',
        content = document.getElementById('art'+rowID)[prop];
    

    http://jsfiddle.net/4Tr5T/2/

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制