Javascript - 点击

Javascript - onclick(Javascript - 点击)

本文介绍了Javascript - 点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

function place_widget(name, properties){//bboxvar pleft = 属性 [0];var ptop = 属性[1];变量宽度 = 属性 [2];变量高度 = 属性 [3];var pright = pleft + 宽度;var pbottom = pleft + 高度;var bbox = [pleft, ptop, pright, pbottom];盒子[名称] = bbox;//身份证var id = 'widget_' + 名称;var editspanid = id + "_editspan";var editbuttonid = id + "_editbutton";var editpaneid = id + "_editpane";//创建元素var div = "

对不起,丑陋的代码:P 有人知道为什么 onclick 功能不起作用吗?

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

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

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

未捕获的异常:[异常...对 WrappedNative 原型对象的非法操作"nsresult:0x8057000c (NS_ERROR_XPC_BAD_OP_ON_WN_PROTO)"位置:本机框架 :: <未知文件名> :: <TOP_LEVEL> ::第 0 行"数据:无]

我不确定这意味着什么.

谢谢!

解决方案

你能不能换个试试:

到:

另外,alert"是你写的函数吗?

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.

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

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?

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

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!

解决方案

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?

这篇关于Javascript - 点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Javascript - 点击

基础教程推荐