0

サイトのカスタム辞書にある場合、すべての href リンクをチェックする必要があるサイトがあります。それらが存在する場合、a-tag の title 属性は、サーバーから取得したテキストに設定する必要があります。私はこのサイトの他の問題に基づいてこれをまとめました(例、テストされていません):

// How to only activate "a href" links?
jQuery('a').mouseover(function() {

  // should be what is between <a href="">this text</a>. Is this correct?
  var dictionaryWord = jQuery(this).text();

  // do a server site call to get description for the word given in the var above
  jQuery.get("/getDescriptionFromDictionaryWord.aspx", { word: dictionaryWord },

    function(data){
      // set title tag for the a-tag actual a-tag
      // will "this" refer to the a-tag with mouseover?
      jQuery(this).attr("title", data);

  });

});

上記に誤りがあるかもしれません。ここ StackOverflow で見つかったさまざまな回答から作成しました。上記よりもサーバーから応答を取得するためのより良い方法はありますか?

それが機能する場合、マウスオーバーでタイトルタグを表示する良い方法だけが必要です。私はそのためのいくつかのパッケージを見てきましたので、それは十分に簡単なはずです。

BR。アンダース

更新(以下の回答に基づく)

// all "a href" do this for the first mouseover "one.()"
jQuery('a[href]').one('mouseover',(function() {


  // get a reference to the clicked + get dictionary word
  var anchor = jQuery(this),
      dictionaryWord = anchor.text();

  // do a server site call to get description for the word given in the var above
  jQuery.get("/getDescriptionFromDictionaryWord.aspx", { word: dictionaryWord },

    function(data){
      // set title tag for the a-tag actual a-tag
      // will "this" refer to the a-tag with mouseover?
      anchor.attr("title", data);

  });

}));
4

4 に答える 4

0

リンクにマウスオーバーするのではなく、リンクごとに1回だけ実行する$('a[href]').one('mouseover',function()...のではなく、使用することもお勧めします。$('a[href]').mouseover(function()...

于 2010-01-27T12:09:03.917 に答える
0

いくつかの改善:

//Match only a with the href attribute
jQuery('a[href]').mouseover(function() {

  var dictionaryWord = jQuery(this).text(),
  //cache the context and use it in the ajax function
  el=jQuery(this);

  jQuery.get("/getDescriptionFromDictionaryWord.aspx", { word: dictionaryWord },

    function(data){
      //You can't use the $(this) use the cached context
      el.attr("title", data);  
  });

});

これはうまくいくはずです。

于 2010-01-27T11:57:52.960 に答える
0

このコードは機能するはずですが、私の意見では、サーバーの負荷を軽減するため、javascript を使用せずにタイトルをロードすることをお勧めします。HTMLで説明を表示したい場合を除きます。この目的では、attr タグはオプションではありません。

于 2010-01-27T11:58:03.180 に答える
0

あなたが書いたものは、getDescriptionFromDictionaryWord.aspx次のようなものであれば動作します:

Response.Clear();
Response.Write("description for word");
Response.End();

つまり、応答全体が表示したい値です。

これを行うより適切な方法は、「Web サービス (asmx)」タイプの新しいファイルを作成し、「Dictionary.asmx」と呼ぶことです。作成された CS/VB ファイルで、コメントアウトされた属性のコメントを外してからScriptService、次のようなメソッドを追加してください。

[WebMethod]
public string GetDescription(string word) {
    // internal logic for dictionary lookup
    return description;
}

そうすれば、次のようなリクエストを送信できます。

$('a[href]').mouseover(function() {

    // the above selector finds all anchors with 'href' attribute

    var anchor = $(this); // get a reference to the clicked item
    var dictionaryWord = anchor.text();

    jQuery.get("/Dictionary.asmx/GetDescription", { word: dictionaryWord }, function(data){
      anchor.attr("title", data.d); // .NET will wrap the response in a 'd' property
    });

});
于 2010-01-27T12:02:22.000 に答える