开发者

How can i replace a tag with video tag

开发者 https://www.devze.com 2023-03-17 05:13 出处:网络
I wa开发者_JAVA百科nt to replace <a href=\"123\" .......>text</a> with <video src=\"123\"..... />

I wa开发者_JAVA百科nt to replace <a href="123" .......>text</a> with <video src="123"..... /> How can i do this with jquery/javascript?


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/


Like this:

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

Hope this helps.


Check out jQuery's replaceAll() method

0

精彩评论

暂无评论...
验证码 换一张
取 消