HTML-encoding lost when attribute read from input field

前端 未结 25 3803
时光说笑
时光说笑 2020-11-21 04:04

I’m using JavaScript to pull a value out from a hidden field and display it in a textbox. The value in the hidden field is encoded.

For example,



        
25条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-21 05:02

    Good answer. Note that if the value to encode is undefined or null with jQuery 1.4.2 you might get errors such as:

    jQuery("

    ").text(value).html is not a function

    OR

    Uncaught TypeError: Object has no method 'html'

    The solution is to modify the function to check for an actual value:

    function htmlEncode(value){ 
        if (value) {
            return jQuery('
    ').text(value).html(); } else { return ''; } }

提交回复
热议问题