Friday, May 6, 2011

Adding css to unordered list <li>

I have a unordered list

 <ul id="List1">
    <li>www.xyz.com</li>
        <li>www.abc.com</li>
</ul>

using jquery, i want to convert this li to a link and add font as underline

I am trying it like this

 $('li').css('font', 'underline').click(function() {
                // how to add hyperlink?
            });
From stackoverflow
  • You should check out the "linkify" method mentioned in this question: http://stackoverflow.com/questions/247479/jquery-text-to-link-script/248901#248901

  • What about something like this? This will turn the elements into actual links, so you don't need to add the underline or onclick handler.

        $('li').each(function(e) {
      $(this).wrapInner('<a href="http://' + $(this).text() + '"></a>');
     });
    

    In your example, the list items were urls, so this will only work if that remains the case.

    Charles : Links are normally underlined by default - so unless you've changed the default style to remove the underlining, that code should result in underlined, clickable links.
    Charles : You can use this syntax: $('li a').css({ "text-decoration": "underline", "color": "#333333" }); If you're changing several CSS attributes, though, I'd usually make a new class and then assign the class to the links.

0 comments:

Post a Comment