1

I am a jquery newbie, long time working with WordPress. I cannot manage to get these two scripts to work; regardless of where I am including them.

After having read about this issue on the Codex , I am aware that I need to change these scripts in order to work with WordPress' no-conflict mode, but I am not sure what exactly do I have to do.

Could anyone help me and explain me how to do this for any further time, as well? Thank you.

 function adjustIframes()
{
  $('iframe').each(function(){
    var
    $this       = $(this),
    proportion  = $this.data( 'proportion' ),
    w           = $this.attr('width'),
    actual_w    = $this.width();

    if ( ! proportion )
    {
        proportion = $this.attr('height') / w;
        $this.data( 'proportion', proportion );
    }

    if ( actual_w != w )
    {
        $this.css( 'height', Math.round( actual_w * proportion ) + 'px' );
    }
  });
}
$(window).on('resize load',adjustIframes);

// DOM ready
     $(function() {

      // Create the dropdown base
      $("<select />").appendTo("nav");

      // Create default option "Go to..."
      $("<option />", {
         "selected": "selected",
         "value"   : "",
         "text"    : "Go to..."
      }).appendTo("nav select");

      // Populate dropdown with menu items
      $("nav a").each(function() {
       var el = $(this);
       $("<option />", {
           "value"   : el.attr("href"),
           "text"    : el.text()
       }).appendTo("nav select");
      });

       // To make dropdown actually work
       // To make more unobtrusive: http://css-tricks.com/4064-unobtrusive-page-changer/
      $("nav select").change(function() {
        window.location = $(this).find("option:selected").val();
      });

     });

if( document.createTouch ) {
    this.addEvent(c[j],'tap',this.tipOver,false);
} else {
    this.addEvent(c[j],'mouseover',this.tipOver,false);
    this.addEvent(c[j],'mouseout',this.tipOut,false);
}
4

1 回答 1

0

将您的 jquery 代码包含在 .ready 函数中。下面是针对 jQuery noConflict 模式修改的代码。

jQuery(document).ready(function($) {
    function adjustIframes()
    {
      $('iframe').each(function(){
        var
        $this       = $(this),
        proportion  = $this.data( 'proportion' ),
        w           = $this.attr('width'),
        actual_w    = $this.width();

        if ( ! proportion )
        {
            proportion = $this.attr('height') / w;
            $this.data( 'proportion', proportion );
        }

        if ( actual_w != w )
        {
            $this.css( 'height', Math.round( actual_w * proportion ) + 'px' );
        }
      });
    }
    $(window).on('resize load',adjustIframes);

    // DOM ready
         $(function() {

          // Create the dropdown base
          $("<select />").appendTo("nav");

          // Create default option "Go to..."
          $("<option />", {
             "selected": "selected",
             "value"   : "",
             "text"    : "Go to..."
          }).appendTo("nav select");

          // Populate dropdown with menu items
          $("nav a").each(function() {
           var el = $(this);
           $("<option />", {
               "value"   : el.attr("href"),
               "text"    : el.text()
           }).appendTo("nav select");
          });

           // To make dropdown actually work
           // To make more unobtrusive: http://css-tricks.com/4064-unobtrusive-page-changer/
          $("nav select").change(function() {
            window.location = $(this).find("option:selected").val();
          });

         });

    if( document.createTouch ) {
        this.addEvent(c[j],'tap',this.tipOver,false);
    } else {
        this.addEvent(c[j],'mouseover',this.tipOver,false);
        this.addEvent(c[j],'mouseout',this.tipOut,false);
    }
});

另外请确保您已将 jQuery 文件包含在您的 wordpress 主题的标题中。

于 2012-09-20T07:12:59.963 回答