简体   繁体   中英

Java script (JQuery) do not work after dynamically changing tag attribute ASP.NET

I have problem with running JS function when value or of rel attribute of anchor tag is changed, anchor look like this:

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

I am changing rel tag value from codebehind:

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

getControlId uses FindControl() to retrive Control from aspx page.

If i comment out part in code behind where I change rel attribute value jQuery works fine. Then jQuery script uses default value of rel

Here is jQuery part where I call function:

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

And functions implementation:

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");
    });         
};

I also have other JavaScript functions witch are working fine, but this one gives me problems. Do you think I should call JS on PageLoad method?

dont knwo more about asp.net , but if rel attribute dynamic add then u have to use live event.

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

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