Escape Quotes In HTML5 Data Attribute Using Javascript

前端 未结 9 2133
春和景丽
春和景丽 2020-12-30 20:47

I\'m using jQuery\'s .data() to work with custom HTML5 data attributes where the value of the attribute needs to be able to contain both single quotes and doubl

9条回答
  •  囚心锁ツ
    2020-12-30 21:12

    If they have to be HTML strings with " and ' and whatnot, why not just make separate HTML elements for them: http://jsfiddle.net/N7XXu/.

    E.g. the HTML:

    a

    She said "WTF" on last night's show.

    in combination with the following JavaScript:

    $('.example').each(function() {
        var correspondingElem = $('.example-data[data-which="'
                                  + $(this).data('which')
                                  + '"]');
        $(this).data('example', correspondingElem.html());
    });
    
    alert($('.example').data('example'));
    

    Of course, hide the .example-data elements.

提交回复
热议问题