问下关于TypeError, Object [object Object] has no method 'appendChild' 的问题

Inuyashazc 2013-02-16 10:16:13

var reader_magazine = $("reader_magazine");
for ( var i = 0; i < typeAll.length; i++) {
for ( var j = 0; j < typeOne.length; j++) {
if (typeAll[i] == typeOne[j]) {
var checkBox = document.createElement("input");
checkBox.setAttribute("type","checkbox");
checkBox.setAttribute("id", typeOne[j]);
checkBox.setAttribute("name", "typeBox");
reader_magazine.appendChild(checkBox);
}
}
}

这是我的js代码部分~~我觉得没什么问题~但是一直会报错TypeError, Object [object Object] has no method 'appendChild' 求指点
...全文
904 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
Inuyashazc 2013-02-18
  • 打赏
  • 举报
回复
引用 6 楼 suciver 的回复:
appendChild是js的对象才有的jquery的对象使用append。楼主检查下你var reader_magazine = $("reader_magazine"); reader_magazine有东西吗。是不是一个html元素
嗯后来我查了下不是这个问题,$("reader_magazine"); 貌似这个不能用的,要用doucument那个方法来获取就可以了
Inuyashazc 2013-02-16
  • 打赏
  • 举报
回复
引用 4 楼 fangmingshijie 的回复:
你这个对象不一样,你用jquery的对象添加js的对象,当然报错了,改为reader_magazine.append(checkBox);或者reader_magazine.appendTo(checkBox);试试
试了~~都不报错了~但是都是跟add一样毫无反应~~
  • 打赏
  • 举报
回复
你这个对象不一样,你用jquery的对象添加js的对象,当然报错了,改为reader_magazine.append(checkBox);或者reader_magazine.appendTo(checkBox);试试
Inuyashazc 2013-02-16
  • 打赏
  • 举报
回复
引用 1 楼 fangmingshijie 的回复:
没有这个方法,改为reader_magazine.add(checkBox);试下
以下我是在另一个页面测试的代码~

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'testcheckBoz.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
    <script type='text/javascript' src='<%=basePath%>/dwr/engine.js'></script>
    <script type='text/javascript' src='<%=basePath%>/dwr/util.js'></script>
	<script type="text/javascript">
    function getEle(){
    	var executerDiv = $("executerDiv");
    	for (var i=0;i<5;i++) {
	    	var checkBox=document.createElement("input");
	        checkBox.setAttribute("type","checkbox");
	        checkBox.setAttribute("id",i);
	        checkBox.setAttribute("name", "typeBox");
	        checkBox.checked = true;
	        executerDiv.appendChild(checkBox);
	        executerDiv.appendChild(document.createTextNode(i));
    	}
    }
    
    function getValue() {
    	var box = document.getElementsByName("typeBox");
    	for (var i = 0 ; i < box.length ; i++) {
    		alert(box[i].id);
    		if (box[i].checked == true) {
    			alert("被选中的:" + box[i].id);
    		}
    	}
    }
    </script>
	
  </head>
  
  <body>
    <div id="executerDiv"></div>
    <input type="button" onclick="getEle()" value="生成checkbox">
    <input type="button" onclick="getValue()" value="跳出选中的checkbox">
  </body>
</html>


Inuyashazc 2013-02-16
  • 打赏
  • 举报
回复
引用 1 楼 fangmingshijie 的回复:
没有这个方法,改为reader_magazine.add(checkBox);试下
我试了改成.add,不报错了,但是不会显示checkbox~~~就是空白的像什么都没发生~ 我之前自己创建过一个很单纯的页面里面就直接用上面的'appendChild' 这个方法,是可以运行的~~但是为什么一到这里就这样了。。。请大神们求指点下~~
  • 打赏
  • 举报
回复
没有这个方法,改为reader_magazine.add(checkBox);试下
suciver 2013-02-16
  • 打赏
  • 举报
回复
appendChild是js的对象才有的jquery的对象使用append。楼主检查下你var reader_magazine = $("reader_magazine"); reader_magazine有东西吗。是不是一个html元素

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧