简体   繁体   English

更改脚本以在div中输入文本,而不是textarea

[英]Change script to enter text in a div, not textarea

I'm working on a job with a developer who is taking care of coding that's beyond my ability; 我正在与一名开发人员一起工作,该开发人员正在处理超出我能力范围的编码。 mostly PHP and more complex Javascript (I'm only okay at jQuery). 主要是PHP和更复杂的JavaScript(我只在 jQuery的)。

Part of the work the developer has been doing involves checking a database and dynamically entering results into the page. 开发人员一直在做的部分工作包括检查数据库并将结果动态输入到页面中。 The problem is that currently these results are entered into a <textarea> , which is not ideal as it means that if it's readonly, the results cannot be deleted if the user changes their mind. 问题在于当前将这些结果输入到<textarea> ,这并不理想,因为这意味着如果它是只读的,则如果用户改变主意,则无法删除结果。 If it's not readonly, the user can enter any text they like into the textarea, rendering the page unusable. 如果不是只读的,则用户可以在文本区域中输入他们喜欢的任何文本,从而使页面无法使用。 In addition, I'm unable to apply any CSS to the results within the textarea. 此外,我无法将任何CSS应用于文本区域内的结果。

Also, the developer is using tables, one of my pet hates. 此外,开发人员正在使用桌子,这是我的宠物讨厌之一。

I would like the <textarea> to be instead a <div> containing a <div> for each database result found. 我希望将<textarea>改为包含找到的每个数据库结果的<div><div>

The developer is currently away for a few weeks and I would prefer to get this done sooner rather than later. 开发人员目前已经离开了几个星期,我希望尽快完成此任务。 I've tried changing "td" simply to "div" but obviously that didn't work. 我尝试将“ td”简单地更改为“ div”,但显然那没有用。

I'd really appreciate some help. 我真的很感谢您的帮助。 I've included the code below. 我已包含以下代码。 Unfortunately, I'm unable to give access to the live/testing site (beyond my control) and apologise for that. 不幸的是,我无法访问现场/测试站点(超出我的控制范围)并为此表示歉意。

if(answer)
{

var newRow = document.getElementById("target_table");
var Row = newRow.rows.length;   
var row1 = newRow.insertRow(Row);
var index=row1.rowIndex;
var td1= document.createElement("TD")
var td2= document.createElement("TD")
td2.innerHTML =  "

<INPUT TYPE='hidden' NAME='course"+Row+"' id='course"+Row+"' value='"+answer+"'>
<INPUT TYPE='hidden' NAME='ary"+Row+"' id='ary"+Row+"'>
<INPUT TYPE='hidden' NAME='Dary"+Row+"' id='Dary"+Row+"'>
<p class='courseName'>"+answer+"</p>
<div id='boxRw"+Row+"' class='boxyHolder'>

    <TABLE border=1 cellspacing=2>
        <TR>
            <TD>
                <INPUT TYPE='button' name='Add a favourite to this course' value='Add a favourite to this course' onclick='javascript: window.open(\"<?=$path?>add-my-favourites/"+Row+"\",\"mywindow\",\"left=550,top=200,width=400,height=350,toolbar=1,resizable=0\");'>
            </TD>
        </TR>
        <tr>
            <td colspan=2>
                <TABLE border=1>
                    <TR>
                        <TD valign=top>Favourites:</TD>
                        <TD>
                            <TEXTAREA class='fav-box' NAME='reps"+Row+"' id='reps"+Row+"' ROWS=4 COLS=45></TEXTAREA>
                            <input class='fav-box' TYPE='hidden' NAME='repsId"+Row+"' id='repsId"+Row+"' value=''>
                        </TD>
                    </TR>
                </TABLE>
            </td>
        </tr>

    </TABLE>

</div>"; 

row1.appendChild(td1);
row1.appendChild(td2);

document.getElementById("h").value=Row;
}

function setVal(){
    var r = "<?=$_GET['hVal']?>";
    var y = "reps"+r;

    var y1 = "repsId"+r;

    window.opener.document.getElementById(y).value+=document.frm.favourites.value+',';
    window.opener.document.getElementById(y1).value+=document.frm.recp_listId.value+',';
    window.close();
}

Edit: I believe these are the lines I need to change: 编辑:我相信这些是我需要更改的行:

window.opener.document.getElementById(y).value+=document.frm.recipes.value+',';             

window.opener.document.getElementById(y1).value+=document.frm.recp_listId.value+',';

I think the problem is that these lines are adding the value to what is currently a <textarea> . 我认为问题在于这些行将值添加到当前的<textarea> When I change the textareas to divs, the values are not applied to the divs. 当我将textareas更改为div时,值不会应用于div。 So I need to change the code so that it enters the results as text in the divs - not values. 因此,我需要更改代码,以便它以divs中的文本(而不是值)形式输入结果。

If you use a div , use its textContent property, not value like you'd use for form controls such as textarea and input . 如果使用div ,请使用其textContent属性,而不要使用用于textareainput等表单控件的value You are getting no error because you're ending up creating a value property on the div . 您没有得到任何错误,因为您最终将在div上创建value属性。


Code snippet #1 代码段#1

var div = document.createElement('div');
div.textContent = document.frm.recipes.valu‌​e;
window.opener.document.getElementById(y).appendChild(div);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM