3

テーブル要素の ID に対応する ID を持つ特定の div をリロードしたい (div にはテーブルの子が 1 つしかない)。

アラートは、tID が定義されていないことを示しています。

JavaScript:

function (msg) {
         var tID = $("table", msg).attr('id');
         alert(tID);
         $("#reloadme_"+tID).html(msg);
}

html:

 <div id="reloadme_2036">

        <table id="2036"  class="customCSSclass">
            ...table contents...
        </table>

   </div>

どこで間違ったのですか?

4

3 に答える 3

3

findjQuery オブジェクト内の要素の現在のセットの子孫を探します.filter。jQuery オブジェクト自体の要素をフィルター処理する whichを使用する必要があります。

$('<table id="001">[...]</table>')
//the jQuery object will contain a reference to the parsed <table> element,
//so you have to .filter() the jQuery object itself to extract it

もちろん、それが jQuery オブジェクト内の唯一の要素である場合は、フィルタリングする必要はありません。=]

また、たとえば、jQuery オブジェクト内で参照される要素の子孫である/ s (または他の要素).findを探すために使用します。trtdtable

于 2013-01-16T02:51:16.123 に答える
1

多分これはあなたが探しているものですか?

function reload(msg) {
  var tID = msg.match(/id="(\d{1,4})"/i)[1]; //find 1 to 4 digits in the id attribute
  alert(tID);  //alerts 2036
  $("#reloadme_"+tID).html(msg); //adds the content to the div
}
reload('<table id="2036" class="customCSSclass"> ...table contents... </table>');

もしそうなら、おそらくあなたが探しているのは、文字列内の ID 番号を見つける JavaScript の .match() メソッドです。

JSFiddleをチェックしてください。

于 2013-01-16T02:49:59.493 に答える
0

あなたはこのように試さなければなりません

var msg='<table id="2036"  class="customCSSclass"></table>';
alert($(msg).attr("id"));
于 2013-01-16T02:53:54.857 に答える