简体   繁体   中英

jQuery error after setting anchor tag dynamically

I am trying to set the href property of an anchor tag using jquery, by writing this code:

 myPage.URL != null ? $pages.find("#myPage").attr("href", myPage.URL.Url) : $pages.find("#myPage").attr("href", "#") ;

Where myPage object contains an object called URL 2 properties: Url and Description. If URL is not null, then for sure it will have a Url value.

After running this code, I try to click on the link, but it's giving this error:

jquery-1.11.3.min.js:2 Uncaught Error: Syntax error, unrecognized expression: http://yourlink.com (…)

Here's myPage object structure:

 pageType : "publishing",
 pageCategory: null 
 Steps : null
 pageOwner: null 
 audience: "All"
 audienceDescription: "description goes here for the audience"
 Title : "my page title"
 URL : Object
    > Description "any link goes here" 
    > Url "http://google.com"

What am I doing wrong?

Thanks.

You need also to check if your object myPage.URL has the property Url then assign it :

if( myPage.URL != null )
     if( myPage.URL.hasOwnProperty("Url") )
           $pages.find("#myPage").attr("href", myPage.URL.Url);
     else
           $pages.find("#myPage").attr("href", "#")

Hope this helps.

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