2011-01-14 68 views
0
$("a[rel='view-show-info']").click(function() { 
    $(this).parent().next('.show-info').slideToggle(70); 

    var img = $("img", this).attr("src"); 
    if(img == "<?=base_url();?>assets/images/plus.png") // or check the end of the path or similar 
    $("img", this).attr("src", "/images/minus.png"); 
    else 
    $("img", this).attr("src", "/images/plus.png"); 

    return false; 
}); 

<a href="#" rel="view-show-info" class="view-more"> 
    <img src="/images/plus.png" alt="expand selection"> 
</a> 

根据用户点击的内容显示加号或减号图像。根据链接状态附加标题以链接(a href)标记

我想为链接添加一个标题,如果图片显示的是minus.png图片,请将title设置为title =“合约选择”,如果显示的是plus.png图片,则将其设置为title = “扩大选择”

因此链接将是这样的:

minus.png:

<a href="#" rel="view-show-info" class="view-more" title="contract selection"> 
    <img src="/images/minus.png" alt="contract selection"> 
</a> 

plus.png

<a href="#" rel="view-show-info" class="view-more" title="expand selection"> 
    <img src="/images/plus.png" alt="expand selection"> 
</a> 

我想我必须使用append函数,但是尽我所知,任何帮助表示赞赏。

回答

2

您可以使用.closest向上遍历树并找到锚(这样你就可以改变它的标题属性):

var src = '/images/plus.png'; 
var title = 'expand selection'; 
if(img == "<?=base_url();?>assets/images/plus.png") 
{ 
    src = '/images/minus.png'; 
    title = 'collapse selection'; 
} 
$("img", this) // grab your image (per normal) 
    .attr({'src':src,'alt':title}) // change the source 
    .closest('a') // go up the three to find the anchor 
    .attr('title',title) // change the anchor's title 

编辑哎呀,你要为锚。
EDIT2改变了一下代码,使其更加简化。现在也改变img alt属性。 (仍在IE上工作)

+0

似乎可以在除IE7以外的任何应用程序中工作。 – Brad 2011-01-14 16:47:09

0
if(img == "<?=base_url();?>assets/images/plus.png"){ 
    $("img", this).attr("src", "/images/minus.png").parent().attr('title', 'contract selection'); 
} else { 
    $("img", this).attr("src", "/images/plus.png").parent().attr('title', 'expand selection'); 
} 
+0

在IE7中不起作用 – Brad 2011-01-14 16:52:20