0

div の id を取得し、それを使用してファイル名を呼び出したいのですが、その方法がわかりません。

たとえば、要素は

<p id="smith_janis" class="name">Janis Smith</p>

などのようなもの

$(".name").click(function() {
  $(this).get the element's id and turn it into variale maybe?

  $("#text").load(the name of the element +".txt"); or $("#text").load("smith_janis.txt");

});

これを行う方法が正確にはわかりません。誰かが私を助けてくれますか? 前もって感謝します。

4

3 に答える 3

1

閉じて、次のようなものを使用します。

$(".name").click(function() {
  var id = $(this).attr('id');
  $("#text").load(id +".txt");
});
于 2012-11-15T22:59:45.563 に答える
1

使用する.attr()

$(".name").click(function() {
    var id = $(this).attr('id') ;  $(this) is a -- jQuery Object 
             **OR**
    var id = this.id ;   this is a -- DOM Object 
    $("#text").load(id + '.txt');

});
于 2012-11-15T22:59:57.827 に答える
1

をご覧くださいjQuery.attr()。ヒント:

var id = $(this).attr('id');
于 2012-11-15T23:00:05.190 に答える