简体   繁体   中英

How can i replace a tag with video tag

I want to replace <a href="123".......>text</a> with <video src="123"..... /> How can i do this with jquery/javascript?

Like this:

$(document).ready(function(){
   var $a = $("a");
   $("<video/>").attr("src", $a.attr("href")).after($a);   
   $a.remove();
});

Hope this helps.

Or I assume you want to replace the a tags after clicking? NOTE: I replace the 'video' tag with div, just for making it easier for you to see the change.

$(document).ready(function(){
    $('.toVideo').live('click', function(e){
        $(this).replaceWith('<div src="' + $(this).attr('href') + '">new video</div>');
        e.preventDefault();  
    })
})

Here is the example: http://jsfiddle.net/a9xHS/5/

Or if you want to replace all tags, maybe you can:

$(document).ready(function(){
    $('.toVideo').each(function(){
        $(this).replaceWith('<div src="' + $(this).attr('href') + '">' + $(this).attr('href') + '</div>');
    })
})

http://jsfiddle.net/A9R3L/1/

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