开发者

Stop asp:Button Text from being HTML encoded

开发者 https://www.devze.com 2023-04-01 09:25 出处:网络
I am maintaining an ASP.NET 3.5 application and I\'d like to add some \"off screen\" html content within the Text property of a Button control in order to make it 开发者_运维百科more accessible, e.g.

I am maintaining an ASP.NET 3.5 application and I'd like to add some "off screen" html content within the Text property of a Button control in order to make it 开发者_运维百科more accessible, e.g.

<asp:Button CssClass="std-text" runat="server" ID="btnChangeLocation"
            OnClick="btnChangeLocation_Click" 
            Text="Set<span class='offscreen'> Location</span>" />

but the Text is always HTML-encoded, how can I stop this?


one way to do that is use a javascript replacement, for example:

<asp:Button CssClass="std-text" runat="server" 
       ID="btnChangeLocation" OnClick="btnChangeLocation_Click" 
       data-text="Set<span class='offscreen'> Location</span>" 
       Text="replace-text" />

and call a replace method on Load like:

$(function() {

    replaceAllTexts();
});

function replaceAllTexts() {

    // for each button
    $("input[type='button']").each(function() {

        // if the text is 'replace-text' and there is a data-text attribute
        if( $(this).text() === "replace-text" && $(this).attr("data-text") ) {
            // replace button's text
            $(this).html( $(this).attr("data-text") );
        }
    });
}
0

精彩评论

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