简体   繁体   中英

jquery change text to hyperlink through all Div tags

I have page say

<div class="wrapper" id="checkLink">
    <div name="something">
       <div name="something">
          <bean:write name="" property=""/>
       </div>
    </div>
    <div name="something">
       <div name="something">
           <div name="something">
                 <bean:write name="" property=""/>
           </div>
       </div>
       </div>
    </div>

I want to change any text within "checkLink" div to a hyperlink if it starts with a http/https/www

The data comes from the back end and is entered by the end users... There are 90 odd fields(Out of which anyone could be a hyperlink)

Only the matching values should appear as hyperlinks

How do I convert them into hyperlinks?

https://jsfiddle.net/w033ucrm/1/

var divs = $('#checkLink div');
$.each(divs, function(index, div){
    var target = $(div).attr('name');
   $('body').append('<a href="http://www'+target+'">'+target+'</a><br/>'); 
});

you could also use $(div).replaceWith() but with nested divs you'd only get 2 anchors

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