Ik ben bezig met een wysiwyg editor in javascript. Op een bepaald moment in de code, voeg ik buttons toe aan de toolbar met behulp van de functie createButton:
Het probleem is als volgt: telkens als ik op een button in de toolbar klik, gaat de browser naar http://www.mijnsite.com/#. Ik wil de href dus disablen en via Google vond ik dat ik dat moet doen met het toevoegen van return false aan de onclick handler:
Maar hoe moet ik de functie createButton aanpassen, zodat deze return false; aan de buttons toevoegt? Ik probeerde al
maar dat werkt niet. Wie kan mij helpen?
JavaScript:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
| txtEditor.prototype.createToolbar = function(idprefix) { // ... this.createButton(theList.id + 'ButtonBold', 'tctButton', 'Bold', 'bold'); this.createButton(theList.id + 'ButtonLink', 'txtButton', 'Hyperlink', 'link'); } txtEditor.prototype.createButton = function(theID, theClass, theLabel, theAction) { var menuItem = document.createElement('li'); var theLink = document.createElement('a'); var theText = document.createTextNode(theLabel); menuItem.id = theID; menuItem.className = 'txtEditButton'; theLink.href = '#'; theLink.title = theLabel; theLink.className = theClass; theLink.action = theAction; var self = this; var onclickHandler = this.toolbarAction; theLink.onclick = function() { onclickHandler.call(self, theAction); } theLink.appendChild(theText); menuItem.appendChild(theLink); return menuItem; } txtEditor.prototype.toolbarAction = function(theAction) { var target = this.textarea; // do stuff with theAction... } |
Het probleem is als volgt: telkens als ik op een button in de toolbar klik, gaat de browser naar http://www.mijnsite.com/#. Ik wil de href dus disablen en via Google vond ik dat ik dat moet doen met het toevoegen van return false aan de onclick handler:
code:
1
| <a href="#" onclick="toolBarAction('bold'); return false;">Bold</a> |
Maar hoe moet ik de functie createButton aanpassen, zodat deze return false; aan de buttons toevoegt? Ik probeerde al
JavaScript:
18
| theLink.action = theAction + ' return false;'; |
maar dat werkt niet. Wie kan mij helpen?
[ Voor 3% gewijzigd door Reveller op 28-12-2006 21:35 ]
"Real software engineers work from 9 to 5, because that is the way the job is described in the formal spec. Working late would feel like using an undocumented external procedure."