body的操作必须要放在window.onload里<script type="text/javascript">
var str = ""
+ " <div id='smart'"
+ " style='"
+ "width:800px;"
+ "height:300px;"
+ "'"
+ ">"
+ "DIV内的文字"
+ "</div>"
window.onload = function () {
document.body.insertAdjacentHTML("afterEnd", str)
};
</script>
var str = ""
+ " <div id='smart'"
+ " style='"
+ "width:800px;"
+ "height:300px;"
+ "'"
+ ">"
+ "DIV内的文字"
+ "</div>"
window.onload = function () {
document.body.insertAdjacentHTML("afterEnd", str)
};
</script>
必须是在你页面加载时操作BODY
var div = document.createElement("div");
div.id = "div1";
div.style.width = 800;
div.style.height = 300;
div.innerText = "DIV内的文字" ;document.appendChild(div);
要找这个div,就 var div = document.getElementById("div1");
<head>
</head>
<script>
function startDrag(obj)
{
alert("dddd");
}function setConfig()
{
var str = ""
+ "<div id=message "
+ "style="
+ "width:50;"
+ "height:60;"
+ "background-color:lightyellow;"
+ "onmousedown='startDrag(this)' "
+ ">"
+"ddd"
+"</div>"
document.body.insertAdjacentHTML("beforeEnd", str); }
</script>
<body>
<input type="button" text="value" value="提交" onClick="setConfig();">
</body>
</html>
我现在加载DIV成功了,可是为什么加载的事件onmousedown='startDrag(this)没有触发
<head>
</head>
<script>
function startDrag(obj)
{
alert("dddd");
}function setConfig()
{
var str = ""
+ "<div id=message "
+ "style="
+ "width:50;"
+ "height:60;"
+ "background-color:lightyellow;"
+ "onmousedown='startDrag(this)' "
+ ">"
+"ddd"
+"</div>"
document.body.insertAdjacentHTML("beforeEnd", str); }
</script>
<body>
<input type="button" text="value" value="提交" onClick="setConfig();">
</body>
</html>
我现在加载DIV成功了,可是为什么加载的事件onmousedown='startDrag(this)没有触发,这是为什么,有什么办法?
div.attachEvent("onmousedown",startDrag);
闪了。。
<script type="text/javascript">
function startDrag(obj) {
alert("dddd");
}var div = [
'<div id="smart" style="width:800px;height:300px;"'
, ' onmousedown="startDrag(this)"'
, '>'
, 'DIV内的文字'
, "</div>"
].join("");window.onload = function () {
document.body.insertAdjacentHTML("beforeEnd", div);
}
</script>
<head>
</head>
<script>
function setConfig()
{
var str =" "
+ "<div id=message"
+ "style= "
+ "width:50;"
+ "height:60;"
+ "background-color:lightyellow;"
+ " onmouseover='startDrag()'"
+ ">"
+ "ddd"
+ " </div>"
document.body.insertAdjacentHTML("beforeEnd",str); }
function startDrag()
{
alert("dddd");
}
</script>
<body>
<input type= "button" text= "value" value= "採岎" onClick="setConfig()">
</body>
</html>
通过测试 注意在onmouseover前面加个空格!!解决及时结帖谢谢!~
<script type="text/javascript">(function (bool) {
if (bool) {
HTMLElement.prototype.insertAdjacentHTML = function (sWhere, sHTML) {
//BlueDestiny
var df; var r = this.ownerDocument.createRange();
switch (String(sWhere).toLowerCase()) {
case "beforebegin":
r.setStartBefore(this);
df = r.createContextualFragment(sHTML);
this.parentNode.insertBefore(df, this);
break;
case "afterbegin":
r.selectNodeContents(this);
r.collapse(true);
df = r.createContextualFragment(sHTML);
this.insertBefore(df, this.firstChild);
break;
case "beforeend":
r.selectNodeContents(this);
r.collapse(false);
df = r.createContextualFragment(sHTML);
this.appendChild(df);
break;
case "afterend":
r.setStartAfter(this);
df = r.createContextualFragment(sHTML);
this.parentNode.insertBefore(df, this.nextSibling);
break;
}
};
}
})(/Firefox/.test(window.navigator.userAgent));function startDrag(obj) {
alert("dddd");
}var div = [
'<div id="smart" style="width:800px;height:200px;'
, 'border:#CCCCCC 1px solid;background-color:#F4F4F4;"'
, ' onmousedown="startDrag(this)"'
, '>'
, 'DIV内的文字'
, "</div>"
].join("");var addDiv = function (id) {
if (!document.getElementById(id)) document.body.insertAdjacentHTML('beforeEnd', div)
};
</script>
<button onclick="addDiv('smart')">添加</button>
<script>
var oDiv = document.createElement("DIV");
oDiv.id = "shop01";
oDiv.style.top = 200;
oDiv.style.left = 200;
oDiv.style.background = '#FFFF00';
oDiv.style.visibility = 'visible';
oDiv.innerHTML="123123"
document.body.appendChild(oDiv);
oDiv.onclick = abc;
function abc(){
alert("abc");
}
</script>
这样也是一个道理。MARK
<script type="text/javascript">(function (bool) {
if (bool) {
HTMLElement.prototype.insertAdjacentHTML = function (sWhere, sHTML) {
//BlueDestiny
var df; var r = this.ownerDocument.createRange();
switch (String(sWhere).toLowerCase()) {
case "beforebegin":
r.setStartBefore(this);
df = r.createContextualFragment(sHTML);
this.parentNode.insertBefore(df, this);
break;
case "afterbegin":
r.selectNodeContents(this);
r.collapse(true);
df = r.createContextualFragment(sHTML);
this.insertBefore(df, this.firstChild);
break;
case "beforeend":
r.selectNodeContents(this);
r.collapse(false);
df = r.createContextualFragment(sHTML);
this.appendChild(df);
break;
case "afterend":
r.setStartAfter(this);
df = r.createContextualFragment(sHTML);
this.parentNode.insertBefore(df, this.nextSibling);
break;
}
};
}
})(/Firefox/.test(window.navigator.userAgent));var startDrag = function (obj) {
alert("dddd");
}var addDiv = function (id) {
var div = [
'<div id="' + id + '" style="width:800px;height:200px;'
, 'border:#CCCCCC 1px solid;background-color:#F4F4F4;"'
, ' onmousedown="startDrag(this)"'
, '>'
, 'DIV内的文字'
, "</div>"
].join("");
if (!document.getElementById(id)) document.body.insertAdjacentHTML('beforeEnd', div);
};
</script>
<button onclick="addDiv('smart')">添加</button>