2

関数を呼び出すかのように、関数に何かを渡すにはどうすればよいでしょうか?

私はこの機能を持っているとしましょう

function ShowId() {
  alert($(this).attr('id'));
}

そしてこのHTML

<div id='div1'>
  <a class='link'>Some link</a>
</div>

やりたい

$('div .link').click(function() {
  $(this).parent('div').call(ShowId); // What is the right syntax?
});

そしてそれは警告するはずです:div1

4

5 に答える 5

4

.call()functions のメソッドであるため、順序が逆になります。

ShowId.call($(this).parent('div'));

ただし、引数として渡すことも通常実行可能です。

function ShowId(elem) {
    alert($(elem).attr('id'));
}
ShowId($(this).parent('div'));
于 2013-07-18T07:19:52.167 に答える
2

ほとんど、これを試してください:

ShowId.call($(this).parent('div'))
于 2013-07-18T07:19:40.063 に答える
0

jQuery 要素を渡します。

function ShowId($this) {
  alert($this.attr('id'));
}

$('div .link').click(function() {
  $(this).parent('div').call(ShowId($(this)));
});
于 2013-07-18T07:19:35.667 に答える
0

次のコードを使用します。

$('div .link').click(function() {
  var parentObj = $(this).parent('div');
  ShowId(parentObj); // call the function with arguments simply
});

そして、あなたの機能は次のようになります:

function ShowId(that) {
  alert($(that).attr('id'));
}
于 2013-07-18T07:20:06.753 に答える
0

関数で要素をオブジェクトとして渡すことができます-

$('div .link').click(function() {
  ShowId($(this).parent('div'));
});

function ShowId(Obj) {
  alert(Obj.attr('id'));
}

これを試して

于 2013-07-18T07:25:46.007 に答える