我想将一个对象作为参数传递给字符串中的Onclick函数.像下面这样的东西:
function myfunction(obj,parentobj){
var o=document.createElement("div");
o.innerHTML='<input type="button" onclick="somelistener(' + obj + ')" />';
parentobj.appendChild(o.firstChild);
}
Run Code Online (Sandbox Code Playgroud)
显然,这不起作用.任何人有任何想法?谢谢!
一个更完整的版本,正如@Austin所建议的那样
<!DOCTYPE html>
<html>
<body>
<style type="text/css">
</style>
<p id="test">test</p>
<p id="objectid"></p>
<script>
function test(s){
document.getElementById("test").innerHTML+=s;
}
function somelistener(obj){
test(obj.id);
}
function myfunction(obj,parentobj){
var o=document.createElement("div");
o.innerHTML='<input type="button" onclick="somelistener(' + obj + ')" />';
o.onclick = function () {
someListener(obj)
}
parentobj.appendChild(o.firstChild);
}
myfunction(document.getElementById("objectid"),document.getElementById("test"));
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud) 我使用 html5 来实现“拖放” http://www.w3schools.com/html/html5_draganddrop.asp。
但我的可投放框相当多,无法全部显示在窗口的显示区域中。因此,当我将一个元素拖到窗口的(例如)底部可下拉框超出显示范围时,如何使窗口自动滚动?
就像下面的网页一样,我想将图像拖放到底部矩形(第五个),该矩形超出了显示区域的范围。当我将图像拖到窗口末尾时,如何使窗口自动滚动到底部?
<!DOCTYPE HTML>
<html>
<head>
<style type="text/css">
#div1,#div2,#div3,#div4,#div5
{
width:350px;
height:200px;
padding:10px;
border:1px solid #aaaaaa;
}
</style>
<script>
function allowDrop(ev)
{
ev.preventDefault();
}
function drag(ev)
{
ev.dataTransfer.setData("Text",ev.target.id);
}
function drop(ev)
{
ev.preventDefault();
var data = ev.dataTransfer.getData("Text");
ev.target.innerHTML += data+"droppedHere"+"<br/>";
}
</script>
</head>
<body>
<p>Drag the W3Schools image into the rectangle:</p>
<img id="drag1" src="http://www.w3schools.com/html/img_logo.gif" draggable="true" ondragstart="drag(event)" width="336" height="69" />
<div id="div1" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
<div id="div2" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
<div id="div3" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
<div id="div4" ondrop="drop(event)" ondragover="allowDrop(event)"></div> …Run Code Online (Sandbox Code Playgroud)