Javascript - 点击

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7923350/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 01:43:58  来源:igfitidea点击:

Javascript - onclick

javascriptonclick

提问by Jeremy

first time posting here, but god know's I use this site to search for problems all the time :P Well, I'm having a problem of my own now that I can't seem to figure out easily searching around Google, and after playing with it for about 2 hours, I've finally decided to post a question and see what you guys think.

第一次在这里发帖,但天知道我一直在使用这个网站来搜索问题 :P 好吧,我现在遇到了自己的问题,因为我似乎无法轻松地在 Google 上搜索,并且在玩过之后用了大约 2 个小时,我终于决定发布一个问题,看看你们的想法。

What I'm trying to accomplish here is to have a button that appears over a div when you hover over it that, when clicked, opens an editing pane. The button appears over the div correctly, but for some reason I cannot seem to make the onclick function work to save my life lol. Here's the code I'm working with. If it's not enough, please let me know and I'll add a little more sauce. :P

我在这里想要完成的是,当您将鼠标悬停在 div 上时,该按钮会出现在 div 上,单击该按钮时会打开一个编辑窗格。该按钮正确显示在 div 上,但由于某种原因,我似乎无法使 onclick 功能工作以挽救我的生命,哈哈。这是我正在使用的代码。如果不够,请告诉我,我会加一点酱汁。:P

function place_widget(name, properties){
//bbox
var pleft = properties[0];
var ptop = properties[1];
var width = properties[2];
var height = properties[3];
var pright = pleft + width;
var pbottom = pleft + height;
var bbox = [pleft, ptop, pright, pbottom];
boxes[name] = bbox;

//ID's
var id = 'widget_' + name;
var editspanid = id + "_editspan";
var editbuttonid = id + "_editbutton";
var editpaneid = id + "_editpane";

//Creating Elements
var div = "<div id='" + id + "' class='widget' onmouseover='widget_hover(event);' onmouseout='widget_out(event);'>";
var editbutton = "<a id='" + editbuttonid + "' href='#'><img onclick='toggleEdit;' src='../images/edit_button.png'></a>";
var editspan = "<span id='" + editspanid + "' class='editspan'>" + editbutton + "</span>";
var editpane = "<span id='" + editpaneid + "' class='editpane'>:)</span>";
div += editspan + editpane + "</div>";
body.innerHTML += div;

//Configuring Elements
var editspanelement = document.getElementById(editspanid);
var editbuttonelement = document.getElementById(editbuttonid);
editbuttonelement.onclick = alert; //Does nothing.
var editpaneelement = document.getElementById(editpaneid);
var mainelement = document.getElementById('widget_' + name);
mainelement.style.left = (pleft + leftmost) + "px";
mainelement.style.top = (ptop + topmost) + "px";
mainelement.style.width = width;
mainelement.style.height = height;
getContentsAJAX(name);
}

Sorry for the ugly code :P Anyone have any idea why the onclick function isn't working?

抱歉代码很丑:P 任何人都知道为什么 onclick 功能不起作用?

Also, a bit of extra info: If I open up firebug and put in :

另外,一些额外的信息:如果我打开 firebug 并输入:

document.getElementById('widget_Text_01_editbutton').onclick = alert;

When I click the button, I get:

当我点击按钮时,我得到:

uncaught exception: [Exception... "Illegal operation on WrappedNative prototype object" nsresult: "0x8057000c (NS_ERROR_XPC_BAD_OP_ON_WN_PROTO)" location: "native frame :: <unknown filename> :: <TOP_LEVEL> :: line 0" data: no]

I'm not exactly sure what that means off hand.

我不确定这意味着什么。

Thanks!

谢谢!

采纳答案by Mike Christensen

Can you try changing:

你可以尝试改变:

<img onclick='toggleEdit;' src='../images/edit_button.png'>

to:

到:

<img onclick='toggleEdit();' src='../images/edit_button.png'>

Also, is "alert" a function you wrote?

另外,“警报”是您编写的功能吗?

回答by jfriend00

Start by changing this:

从改变这个开始:

editbuttonelement.onclick = alert; //Does nothing.

to this:

对此:

editbuttonelement.onclick = function() {alert("Got a click");};

and then change this:

然后改变这个:

var editbutton = "<a id='" + editbuttonid + "' href='#'><img onclick='toggleEdit;' src='../images/edit_button.png'></a>";

to this:

对此:

var editbutton = "<a id='" + editbuttonid + "' href='#'><img onclick='toggleEdit();' src='../images/edit_button.png'></a>";

What you are clicking on? The image or the link or the span? Do you know which is getting the click event?

你在点击什么?图像或链接或跨度?你知道哪个是点击事件吗?

回答by Esailija

document.getElementById('widget_Text_01_editbutton').onclick = alert;

causes illegal invocationbecause it is trying to set thisto something else (the element) than windowinside alert.

原因illegal invocation是因为它试图设置this为除windowinside之外的其他内容(元素)alert

alert.call( {} )
//TypeError: Illegal invocation

alert.bind( window ).call( {} )
//works

So either:

所以要么:

document.getElementById('widget_Text_01_editbutton').onclick = alert.bind( window );

or even better as the above will only work in some browsers:

甚至更好,因为以上仅适用于某些浏览器:

document.getElementById('widget_Text_01_editbutton').onclick = function(){alert();};