简体   繁体   中英

Dynamically add title attribute to URL in HTML

I'm trying to create a script to dynamically add a title attribute to individual URLs.

The idea is as follows:

  • User pastes HTML with links into text area (Text area A)
  • User adds a list of titles and URL's into another text area (Text area B) in the following format:

    Title attribute - HREF
    Title attribute - HREF

  • Script then goes through Text area B and appends the title attribute to the relevant link in text area A.

Can anyone guide me on the best way to do this? I don't really know where to start.

Do you want someting like this?

http://jsfiddle.net/ghahvbf4/2/

HTML<br/>
<textarea id="areaA"></textarea><br/>
List<br/>
<textarea id="areaB"></textarea>
<br/>
<div id="myCont" style="height:0;overflow:hidden"></div>
<button id="change">add</button><br/>
Result<br/>
<textarea id="result""></textarea>

$(document).ready(function(){
    $("#change").click(function(){
        $("#myCont").html($("#areaA").val());
        $titles = $("#areaB").val().split("\n");
        $counter = 0;
        $("#myCont a").each(function(x,element){
            if($titles[$counter]!=null){
                $(element).attr("title",$titles[$counter]);
            }
            $counter++
        })
        $("#result").val( $("#myCont").html());
    })

})

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