开发者

jQuery onclick with parameters gives "** is not defined"

开发者 https://www.devze.com 2023-01-18 06:28 出处:网络
This is in my Javascript: function doSomething(fieldActions) { var Actions 开发者_开发百科= fieldActions;

This is in my Javascript:

function doSomething(fieldActions) {
var Actions 开发者_开发百科= fieldActions;

$(Actions).show("slow");
$(this).hide(); }

This is in my HTML:

<a onclick="doSomething('#date-actions');">edit</a>

The parameter needs to affect and DIV field. The ID of this field can be variable and there can be multiple on a page, so that's why I need to give the specific field it affects on. The parameter "#date-actions" is an example of what a DIV field could be called that it is calling to.

My error in firebug:

doSomething is not defined


Currently wherever doSomething is defined it's not a globally accessible function, for example if it's inside a document.ready handler it's only accessible inside there. You would need to expose the scope...but even then this wouldn't refer to what you wanted inside the function.


A better approach to solve both issues would be to use the href property of the link itself, like this:

 <a class="edit" href="#date-actions">edit</a>

Then by giving it a class, like in the above example, you can bind all those links on document.ready like this:

$(function() {
  $("a.edit").click(function(e) {
    $(this).hide();
    $(this.hash).show();
    e.preventDefault();
  });
});

If they're added dynamically just change the bind from $("a.edit").click(function(e) { to $("a.edit").live("click", function(e) {.

0

精彩评论

暂无评论...
验证码 换一张
取 消