1

我正在创建一个动态 Div,当它关闭时我可以从showModalDialog导入值。所以在关闭模式后,我得到了几个值。

我在这里要做的是:

  1. 我有几个动态 div 和针对每个 div,我有一个链接来打开一个窗口。
  2. 选择文件后,它们以逗号分隔返回父窗口。
  3. 我想将这些值插入到打开该弹出窗口的 div 中。但在这种情况下,我面临着麻烦。Divid 是动态生成的

这是基于Javascript + Jquery的完整代码,我收到以下错误。

TypeError: theDiv.appendChild is not a function
[Break On This Error]   

theDiv.appendChild(newNode);

    <script type="text/javascript" src="JS/jquery-1.7.2.min.js"></script>
<script type="text/javascript">
function eliminateDuplicates(arr,divID) 
{   
    var i,       
    len=arr.length,       
    out=[],       
    obj={};    
    for (i=0;i<len;i++) 
    {     
        obj[arr[i]]=0;   
    }   
    for (i in obj) 
    {     
        out.push(i);   
    }   
    return out; 
}
function GetElementsStartingWith(tagName, subString) {
    var elements = document.getElementsByTagName(tagName);
    var result = [];
    for (var i = 0; i < elements.length; i++) {
        var element = elements[i];
        if (element.id && element.id.substr(0, subString.length) == subString) {
            result.push(element);
        }
    }
    return result;
}
Test= function(str,divID) 
{
    var arrID = new Array();
    arrID = str.split(',');                  
    arrID = eliminateDuplicates(arrID);            
    var theDiv = $("#projectsList"+divID).attr('id'); //document.getElementById('projectsList'); 
    alert(theDiv);
    var cmp= $("#projectIDS"+divID).val(); //document.getElementById("projectIDS").value;
    var cnp = $("#countProj"+divID);//document.getElementById("countProj")  
    var cproj;
        if(cnp.val().length == 0)
            cproj=0;
        else
         cproj=parseInt(cnp.val());

    for (var j=0; j<arrID.length; j++)
    {  
        if (parseInt(cproj) + 1 > 50)
        { 
            alert("You cannot add more than 50 Project id's ");
            return;
        }
        if( cmp!="" && cmp.indexOf(arrID[j])!=-1)
        continue;
        var newNode = document.createElement('div');  
        newNode.style.cssText = "background:#CCCCCC;border:1px solid #666666;width:100px;word-wrap:break-word;margin:3px;float:left;color:black;text-decoration:none!important;height:auto;vertical-align:middle;padding-top:2px;";    
        newNode.title = arrID[j]+" ";
        newNode.innerHTML = '<input type=hidden name=Proj_' + j + ' ' + 'value=' + arrID[j] + '>' + arrID[j] + ' <a href="#" title="Remove Project" onclick="removetext($(this).parent());"><b>X</b></a>';   
        theDiv.appendChild(newNode);
        if(cmp.length == 0) 
            {
                //document.getElementById("projectIDS").value=arrID[j]
                $("#projectIDS"+divID).val(arrID[j]);
            }
        else
            {
                //document.getElementById("projectIDS").value = document.getElementById("projectIDS").value+","+arrID[j];
                $("#projectIDS"+divID).val($("#projectIDS"+divID).val()+","+arrID[j]);
            }
            cproj = parseInt(cproj)+1;  
        //document.getElementById("countProj").value =cproj; 
        cnp.value(cproj);
    }  
}
removetext = function(par)
{
    var strremove=par.text();
    var strexist = document.getElementById("projectIDS").value;
    strremove = strremove.replace(" X","");
    tempRemove(strexist, strremove);
    par.remove();
    var cproj;
        if(document.getElementById("countProj").value.length == 0)
            cproj=0;
        else
         {cproj=parseInt(document.getElementById('countProj').value);

        cproj=parseInt(cproj)-1;}                       
        document.getElementById("countProj").value =cproj;   
}

function tempRemove(strexist,strremove)
{
    var b = strexist.indexOf(strremove);
    var after = strexist.indexOf(",",b);
    var newstrexist;
    var before = strexist.lastIndexOf(",",b);
    if(after!=-1)
    {newstrexist=strexist.replace(strremove+',',"");}
    else if(before!=-1)
    {newstrexist=strexist.replace(','+strremove,"");}
    else
    {newstrexist= strexist.replace(strremove,"");}

    document.getElementById("projectIDS").value=newstrexist;
    //remove current friend
}
function openWindow(divID)
{
    var lookUpAlys=window.showModalDialog("files.cfm?d=" + Math.random() + '&fileID=' + divID,window,"center=yes;dialogWidth:895px:dialogHeight:785px;status:no");
    if(lookUpAlys.forename!=undefined)  
    { 
        var temp = lookUpAlys.forename;
        Test(temp,divID);
    }
}
</script>
</head>
<body>
<table width="100%" border="0" cellspacing="2" cellpadding="1">
  <tr>
    <td>Choose</td>
    <td>Files</td>
    <td>Action</td>
  </tr>
  <cfloop from="1" to="5" index="i">
  <cfoutput>
  <tr>
    <td><input type="checkbox" name="getFile" id="getFile" value="#i#" /></td>
    <td><div id="projectsList#i#" style="width:500px;height:60px;overflow-y:scroll;border:1px solid gray;"></div><input type="text" name="projectIDS#i#" id="projectIDS#i#" data-id="#i#" value="" /><input type="text" data-id="#i#" name="countProj#i#" id="countProj#i#" value="" /></td>
    <td><a href="javascript:void(0);" onclick="openWindow(#i#);">Files</a></td>
  </tr>
  </cfoutput>
  </cfloop>
</table>
</body>
</html>

因此,如果我输入的代码不正确,我深表歉意。基本上尝试做经典的Javascript方式

4

2 回答 2

2

这并不像我认为你认为的那样:

var theDiv = $("#projectsList"+divID).attr('id'); //document.getElementById('projectsList'); 

你应该做

var theDiv = $("#projectsList"+divID)[0];

获取 DOM 元素。

或者,对于这种情况,只需执行

var theDiv = document.getElementById("projectsList" + divID);

另外,我不确定您为什么到处混合原始 DOM 操作和 jQuery 包装操作。只需坚持其中之一,并保持一致。

于 2013-06-14T07:41:58.590 回答
0
var container = $('.itemsList');
var divSubmit = $(document.createElement('div'));
//assigning id to div
$(divSubmit).attr('id','itemTemplate');
$(divSubmit).css({"font-family":"Gotham, Helvetica Neue, Helvetica, Arial, sans-serif","position":"relative","height": "70px","clear" : "both","background-color":"#FFF","border-bottom": "0.09em solid #EBEBEB"});
//adding class to main container
$(divSubmit).addClass('itemTemplate');
//adding Child's name label and assigning id to it.
var Name = '<label class="lblName" id="lblName" for="Name">'+getName()+'</label>';
$(divSubmit).append(Name);
$(divSubmit).append(container);

这是一个示例代码。首先有一个名为 itemslist 的示例容器,它将包含生成的 div。divSubmit 将动态生成并附加到容器中。为点击事件找到一些 div。假设我们想要获得孩子的名字。

alert($($(this).find("label.lblName")).val());
于 2015-03-01T20:41:15.513 回答