简体   繁体   中英

JQuery Issue with Internet Explorer

I'm using JQuery in a Wordpress site (Thesis theme) to dynamically swap images. Everything works as expected in Chrome/Firefox/Safari, but the images don't show up at all in IE. Where have I gone wrong? Code below, dev site at daf.drivechannelcreative.com/about .

    function add_image_header(){
    global $post;

    $image_header = get_post_meta( $post->ID, 'image_header', true );
    $image_one_full = get_post_meta( $post->ID, 'image_one_full', true );
    $image_one_cropped = get_post_meta( $post->ID, 'image_one_cropped', true );
    $image_two_full = get_post_meta( $post->ID, 'image_two_full', true );
    $image_two_cropped = get_post_meta( $post->ID, 'image_two_cropped', true );
    $image_three_full = get_post_meta( $post->ID, 'image_three_full', true );
    $image_three_cropped = get_post_meta( $post->ID, 'image_three_cropped', true );

    $page_meta_desc = get_post_meta( $post->ID, 'thesis_description', true );

    if($image_header){
        ?>  
            <script type="text/javascript">
                $(document).ready(function(){
                $(".thumb").click(function(){
                   var Image1Main = $(this).data('main');
                   var Image1Thumb = $(this).attr('src');

                   var Image2Main = $('#main_image').attr('src');
                   var Image2Thumb = $('#main_image').data('thumb');

                   $('#main_image').attr("src", Image1Main);
                   $('#main_image').data("thumb", Image1Thumb);


                   $(this).attr("src", Image2Thumb);
                   $(this).data("main", Image2Main);
                });
            });
            </script>

            <div id="img_header_container">
                <img data-thumb="<?php echo $image_one_cropped;?>" src="<?php echo $image_one_full;?>" id="main_image"/>
                <img class="thumb" data-main="<?php echo $image_two_full;?>" src="<?php echo $image_two_cropped;?>"/>
                <div id="heading_text"><h2><?php echo get_the_title($ID) ?></h2><?php echo $page_meta_desc;?></div>
                <img class="thumb thumb_two" data-main="<?php echo $image_three_full;?>" src="<?php echo $image_three_cropped;?>"/>
            </div>
        <?php
    }
}
add_action('thesis_hook_before_post_box', 'add_image_header');

To set the "src" attribute with jQuery since 1.6, you need to use ".prop()", not ".attr()":

               $(this).prop("src", Image2Thumb);

Seems like a simple thing but it makes a difference now.

Using ".attr()" with just one argument to get the value is possibly OK but even then you're better off with ".prop()".

edit — Brad Christie correctly points out that simply:

               this.src = Image2Thumb;

woks great when your jQuery object is just a single element (like in the code above). The jQuery form is useful if you're setting a zillion different elements.

This is what a piece of your generated HTML looks like:

<img class="thumb" data-main="http://daf.dev/wp-content/uploads/2011/12/image_two_full_example1.jpg" src="http://daf.dev/wp-content/uploads/2011/12/image_two_crop_example1.jpg"/>

The value in the data-main attribute is not a working image URL and that's one of the values that you're going to set the .src value to for your image tag. Somehow, I don't think you're generating the right URLs or the images are present at those URLs or this webpage doesn't work for outsiders like us (I'm not sure which).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM