How to style the string of an tag?

前端 未结 6 1270
孤独总比滥情好
孤独总比滥情好 2020-12-11 09:08

Is there a way to style the alt? With javascript? With pseudo attributes? like this:

\"<h1This is the caption&l         


        
6条回答
  •  隐瞒了意图╮
    2020-12-11 09:58

    No, you can't nest tags inside the attribute value as you have done. However, you can use JavaScript/JQuery to extract the value into a DIV and style that.

    I wrote a simple plugin to do just that, with a bit of hacking it'll do what you want I think.

    Edit: What you could do (though it's a bit hacky) is to use the following in your alt attribute:

    This is the caption|This is some text
    

    Then in whatever code you're using, you can split on the pipe character and surround each part with the appropriate tags, something like this:

    var img = $('#myimg');
    var alt = img.attr('alt').split('|');
    
    var divContents = '

    ' + alt[0] + '

    ' + alt[1] + '

    ';

提交回复
热议问题