如何使用prototype js改变href属性?

发布于 2024-10-16 00:53:35 字数 4308 浏览 2 评论 0原文

我有这个 php 代码..

    <?php
        $a_source = $this->helper('catalog/image')->init($_product, 'image');
        $_img_a = '<a id="swapper" class="thickbox" href="'.$a_source.'" title="'.$this->htmlEscape($this->getImageLabel()).'">';
        $_img_b = '<img id="image" src="'.$this->helper('catalog/image')->init($_product, 'image').'" alt="'.$this->htmlEscape($this->getImageLabel()).'" title="'.$this->htmlEscape($this->getImageLabel()).'" /></a>';
    ?>

我想使用原型更改 href 的值..

$('swapper').writeAttribute('href', url);

但它不会改变..我在这里遗漏了什么吗?谢谢! 顺便说一句,这是 magento media.phtml 的完整代码

<?php
    $_product = $this->getProduct();
    $_helper = $this->helper('catalog/output');
    $_gallery = $this->getGalleryImages();

    $_resize = 350;
?>

<style type="text/css">
    .product-img-box .more-views li.slide-current a{ border:2px solid #aaa; }
    .product-img-box .product-image-zoom img { cursor: pointer; }
    #slide-loader{ visibility:hidden; position:absolute; top:auto; left:auto; right:2px; bottom:2px; width: 25px; height: 25px; }
</style>

<script type="text/javascript">
function slide(url,num,gallery){
    if (typeof slide.loading == 'undefined') slide.loading = false;
    if(slide.loading) return false;


    var loader = new Image();
    $(loader).observe('load', function(){
        $('slide-loader').setStyle({'visibility':'hidden'});

        $$('div.more-views li').each(function(el,i){
            (i==num) ? el.addClassName('slide-current') : el.removeClassName('slide-current');
        });

        var swap_link = new Element
        var dummy = new Element('img', { src: url }).setOpacity(0);
        new Insertion.After('image', dummy);
        new Effect.Opacity(dummy, { duration:.5, from:0, to:1.0 });
        new Effect.Opacity($('image'), { duration:.5, from:1.0, to:0, 
             afterFinish: function(){

                $('image').writeAttribute('src',url).setOpacity(1).observe('click',function(){
                    //Event.stop(e);
                    $('swapper').writeAttribute('href', url);


                 })
                dummy.remove();
                slide.loading = false;
             }
        });
    });

    $('slide-loader').setStyle({'visibility':'visible'});
    loader.src=url;

    slide.loading = true;
return false;
}
</script>


<p class="product-image-zoom">

    <?php
        $a_source = $this->helper('catalog/image')->init($_product, 'image');
        $_img_a = '<a id="swapper" class="thickbox" href="'.$a_source.'" title="'.$this->htmlEscape($this->getImageLabel()).'">';
        $_img_b = '<img id="image" src="'.$this->helper('catalog/image')->init($_product, 'image').'" alt="'.$this->htmlEscape($this->getImageLabel()).'" title="'.$this->htmlEscape($this->getImageLabel()).'" /></a>';
    ?>

    <?php echo $_helper->productAttribute($_product, $_img_a.$_img_b, 'image') ?>
    <img id="slide-loader" src="<?php echo $this->getSkinUrl('images/lightbox/loading.gif') ?>" />

</p>

<?php if (count($_gallery) > 0): ?>
<div class="more-views">
    <h4><?php echo $this->__('More Views') ?></h4>
    <ul>
    <?php foreach ($_gallery as $_image): ?>
        <li>
            <a href="<?php echo $this->helper('catalog/image')->init($this->getProduct(), 'image', $_image->getFile()); ?>" onclick="slide('<?php echo $this->helper('catalog/image')->init($this->getProduct(), 'image', $_image->getFile()) ?>',<?php echo ($s = isset($s) ? ++$s : 0) ?>,'<?php echo $this->getGalleryUrl($_image) ?>'); return false;"><img src="<?php echo $this->helper('catalog/image')->init($this->getProduct(), 'thumbnail', $_image->getFile())->resize(65); ?>" alt="<?php echo $this->htmlEscape($_image->getLabel()) ?>" title="<?php echo $this->htmlEscape($_image->getLabel()) ?>" /></a>

        </li>
    <?php endforeach; ?>
    </ul>
</div>

<?php endif; ?>

I have this php code..

    <?php
        $a_source = $this->helper('catalog/image')->init($_product, 'image');
        $_img_a = '<a id="swapper" class="thickbox" href="'.$a_source.'" title="'.$this->htmlEscape($this->getImageLabel()).'">';
        $_img_b = '<img id="image" src="'.$this->helper('catalog/image')->init($_product, 'image').'" alt="'.$this->htmlEscape($this->getImageLabel()).'" title="'.$this->htmlEscape($this->getImageLabel()).'" /></a>';
    ?>

I want to change the value of href using prototype..

$('swapper').writeAttribute('href', url);

but it won't change.. Am I missing something here? thanks!
by the way here's the whole code from magento media.phtml

<?php
    $_product = $this->getProduct();
    $_helper = $this->helper('catalog/output');
    $_gallery = $this->getGalleryImages();

    $_resize = 350;
?>

<style type="text/css">
    .product-img-box .more-views li.slide-current a{ border:2px solid #aaa; }
    .product-img-box .product-image-zoom img { cursor: pointer; }
    #slide-loader{ visibility:hidden; position:absolute; top:auto; left:auto; right:2px; bottom:2px; width: 25px; height: 25px; }
</style>

<script type="text/javascript">
function slide(url,num,gallery){
    if (typeof slide.loading == 'undefined') slide.loading = false;
    if(slide.loading) return false;


    var loader = new Image();
    $(loader).observe('load', function(){
        $('slide-loader').setStyle({'visibility':'hidden'});

        $('div.more-views li').each(function(el,i){
            (i==num) ? el.addClassName('slide-current') : el.removeClassName('slide-current');
        });

        var swap_link = new Element
        var dummy = new Element('img', { src: url }).setOpacity(0);
        new Insertion.After('image', dummy);
        new Effect.Opacity(dummy, { duration:.5, from:0, to:1.0 });
        new Effect.Opacity($('image'), { duration:.5, from:1.0, to:0, 
             afterFinish: function(){

                $('image').writeAttribute('src',url).setOpacity(1).observe('click',function(){
                    //Event.stop(e);
                    $('swapper').writeAttribute('href', url);


                 })
                dummy.remove();
                slide.loading = false;
             }
        });
    });

    $('slide-loader').setStyle({'visibility':'visible'});
    loader.src=url;

    slide.loading = true;
return false;
}
</script>


<p class="product-image-zoom">

    <?php
        $a_source = $this->helper('catalog/image')->init($_product, 'image');
        $_img_a = '<a id="swapper" class="thickbox" href="'.$a_source.'" title="'.$this->htmlEscape($this->getImageLabel()).'">';
        $_img_b = '<img id="image" src="'.$this->helper('catalog/image')->init($_product, 'image').'" alt="'.$this->htmlEscape($this->getImageLabel()).'" title="'.$this->htmlEscape($this->getImageLabel()).'" /></a>';
    ?>

    <?php echo $_helper->productAttribute($_product, $_img_a.$_img_b, 'image') ?>
    <img id="slide-loader" src="<?php echo $this->getSkinUrl('images/lightbox/loading.gif') ?>" />

</p>

<?php if (count($_gallery) > 0): ?>
<div class="more-views">
    <h4><?php echo $this->__('More Views') ?></h4>
    <ul>
    <?php foreach ($_gallery as $_image): ?>
        <li>
            <a href="<?php echo $this->helper('catalog/image')->init($this->getProduct(), 'image', $_image->getFile()); ?>" onclick="slide('<?php echo $this->helper('catalog/image')->init($this->getProduct(), 'image', $_image->getFile()) ?>',<?php echo ($s = isset($s) ? ++$s : 0) ?>,'<?php echo $this->getGalleryUrl($_image) ?>'); return false;"><img src="<?php echo $this->helper('catalog/image')->init($this->getProduct(), 'thumbnail', $_image->getFile())->resize(65); ?>" alt="<?php echo $this->htmlEscape($_image->getLabel()) ?>" title="<?php echo $this->htmlEscape($_image->getLabel()) ?>" /></a>

        </li>
    <?php endforeach; ?>
    </ul>
</div>

<?php endif; ?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

予囚 2024-10-23 00:53:35

它应该是 setAttribute

$('swapper').setAttribute('href', url);

您可以直接访问 href 属性,如下所示:

$('swapper').href = url;

在这里尝试一下。

It should be setAttribute:

$('swapper').setAttribute('href', url);

You can directly access the href attribute like this:

$('swapper').href = url;

Try them here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文