0

当标签的值或rel属性发生更改时,我在运行 JS 函数时遇到问题,如下所示:anchoranchor

<a id="ThemeImageLink1" runat="server" href="javascript:void(0)" class="preview"
rel="../Images/templateChooser/preview/preview1.jpg">

我正在从代码隐藏中更改 rel 标记值:

HtmlAnchor imageLink = (HtmlAnchor)getControlId(counter, "ThemeImageLink");
imageLink.Attributes.Add("rel","../Images/templateChooser/preview/preview2.jpg");

getControlId用于FindControl()aspx页面检索控制。

如果我在更改属性值的地方注释掉代码中的部分,则reljQuery 可以正常工作。然后jQuery脚本使用默认值rel

这是我调用函数的 jQuery 部分:

    $(document).ready(function () {
         imagePreview();
    });

及功能实现:

this.imagePreview = function(){ 

        xOffset = 10;
        yOffset = 30;

    $("a.preview").hover(function(e){
        this.t = this.title;
        this.title = "";    
        var c = (this.t != "") ? "<br/>" + this.t : "";
        $("body").append("<p id='preview'><img src='"+ this.rel +"' alt='Image preview' />"+ c +"</p>");                                 
        $("#preview")
            .css("top",(e.pageY - xOffset) + "px")
            .css("left",(e.pageX + yOffset) + "px")
            .fadeIn("fast");    
    },
    function(){
        this.title = this.t;    
        $("#preview").remove();
    }); 
    $("a.preview").mousemove(function(e){
        $("#preview")
            .css("top",(e.pageY - xOffset) + "px")
            .css("left",(e.pageX + yOffset) + "px");
    });         
};

我也有其他 JavaScript 函数可以正常工作,但是这个给我带来了问题。你认为我应该在 PageLoad 方法上调用 JS 吗?

4

1 回答 1

0

不知道更多关于 asp.net 的信息,但如果 rel 属性动态添加,那么你必须使用实时事件。

喜欢 $("a.preview").live('hover' , function(e){

于 2012-03-05T10:26:44.200 回答