2

プロトタイプを作成する関数を使用せずに最初にコードを記述しましたが、もちろん、正常に機能しました。

$(function() {
    $(".PortfolioFade img")
        .mouseover(function() { 
            popup('PORTFOLIO');
            var src = $(this).attr("src").replace("images/paperclip.png", "images/paperclip-black.png");
            /*var src = $(this).attr("src").match(/[^\.]+/) + "-black.png";*/
            $(this).attr("src", src);
        })
        .mouseout(function() {
            ;
            /*var src = $(this).attr("src").replace("images/paperclip-black.png", "images/paperclip.png");
            $(this).attr("src", src); Look at popup.js mouseover events*/ 
        });
    });

しかし、同じことを関数形式で表現したところ、関数呼び出しが機能していないようでした。

$(document).ready(function() {
   // put all your jQuery goodness in here.
   $('body').hide().fadeIn(1000);

    function ImageRollover(image_element, popup_name, original, replacement)
{
    $(element)
        .mouseover(function(){
            popup(popup_name);
            var src = $(this).attr("src").replace(original,replacement);
            $(this).attr("src",src);

        })
        .mouseout(function(){
            ;
        });
}

   ImageRollover(".Portfolio img",'PORTFOLIO',"images/paperclip.png","images/paperclip-black.png"); 
 });

他の場所で関数を定義しても、効果はないようです。

4

3 に答える 3

3

これはあなたが達成しようとしていることですか?

function ImageRollover(element, popup, original, replacement)
{
    $(element).mouseover(function(){
            //popup(element);
            //var src = $(this).attr("src").replace(original,replacement);
            $(this).attr("src",replacement);

        })
        .mouseout(function(){
            $(this).attr("src",original);
        });
}

http://jsfiddle.net/SqyDg/

于 2012-06-18T02:18:27.367 に答える
2

関数は最初の変数をとして定義してimage_elementいますが、コード内でそれを参照していelementます。それが機能しない要因の1つである可能性が非常に高いです。

関数内のキーワードにも問題が発生する可能性がありthisます。元のコード(jQueryがHTML要素に設定する)と同じオブジェクトを参照していません。関数では、何にも設定されていない可能性が高いため、へのリンクwindowです。

于 2012-06-18T02:10:49.957 に答える
2
    function ImageRollover(image_element, popup_name, original, replacement)
{
    $(element)

要素はどこで定義されていますか?

あなたが意味するかもしれません:

    function ImageRollover(image_element, popup_name, original, replacement)
{
    $(image_element)
于 2012-06-18T02:11:36.883 に答える