Ruby on rails Rails文本区域在Internet Explorer中将占位符显示为文本区域内容

Ruby on rails Rails文本区域在Internet Explorer中将占位符显示为文本区域内容,ruby-on-rails,internet-explorer-11,Ruby On Rails,Internet Explorer 11,我不确定这是如何发生的,但占位符似乎是将内容放在文本区域中,而不是Internet Explorer v11中的占位符 Internet Explorer v11 铬 这是rails的文本区号 = f.text_area :qhse_rules_comment, class: 'form-control audit_area', rows: '2', placeholder: t('.please_enter_comments') 如何使Internet Explorer不将占位符用作文本

我不确定这是如何发生的,但占位符似乎是将内容放在文本区域中,而不是Internet Explorer v11中的占位符

Internet Explorer v11

这是rails的文本区号

= f.text_area :qhse_rules_comment, class: 'form-control audit_area', rows: '2', placeholder: t('.please_enter_comments')

如何使Internet Explorer不将占位符用作文本区域内容?

是的,IE存在此问题

您可以使用gem“jquery占位符rails”来解决这个问题


是的,IE有这个问题

您可以使用gem“jquery占位符rails”来解决这个问题


这是一个已知的IE bug,已被跟踪。使用JS解决方案将是解决这一问题的最佳方法,例如

/**
 * Returns a "bug free" html string from a jQuery element. Takes care of an IE (>=10) bug which occurs when a textarea has a placeholder attribute.
 *
 * The problem is when a textarea in IE (>=10) has a placeholder attribute, IE also adds the value of this attribute as text/value to the textarea.
 * Which means that .html() of such an element returns something like <textarea placeholder="foo">foo</textarea> (but should be <textarea placeholder="foo"></textarea>),
 * therefore $($target.html()) would return a textarea which now has value (which is the placeholder)!
 */


var tidyHtml = function($target) {
    var cleanHtml = $target.html(), // variable now contains "buggy" textareas
        badIndizes = [];

    $target.find('textarea[placeholder]').each(function(index, element) {
        if($(element).text() !== $(element).val()) { // there is something in .text(), but no value exists! IE bug! (do NOT use .html() here, because it's escaped -> val() is not escaped)
            badIndizes.push(index);
        }
    });

    if(badIndizes.length > 0) {
        cleanHtml = $(cleanHtml).find('textarea[placeholder]').filter(function(index) {
            return $.inArray(index, badIndizes) > -1;
        }).html('').parents().last().wrap('<p>').parent().html();
    }

    return cleanHtml;
};
/**
*从jQuery元素返回“无错误”html字符串。处理当textarea具有占位符属性时发生的IE(>=10)错误。
*
*问题是当IE(>=10)中的textarea具有占位符属性时,IE还会将该属性的值作为文本/值添加到textarea。
*这意味着此类元素的.html()返回类似于foo的内容(但应该是),
*因此,$($target.html())将返回一个现在具有值(即占位符)的textarea!
*/
var tidyHtml=函数($target){
var cleanHtml=$target.html(),//变量现在包含“buggy”文本区域
badIndizes=[];
$target.find('textarea[占位符])。每个(函数(索引,元素){
if($(element).text()!==$(element).val()){//在.text()中有某些内容,但不存在任何值!即bug!(此处不要使用.html(),因为它已转义->val()未转义)
badIndizes.push(索引);
}
});
如果(badIndizes.length>0){
cleanHtml=$(cleanHtml).find('textarea[占位符]).filter(函数(索引){
返回$.inArray(索引,badIndizes)>-1;
}).html(“”).parents().last().wrap(“”).parent().html();
}
返回干净的HTML;
};

credit-jamez1414这是一个已知的IE漏洞,已被跟踪。使用JS解决方案将是解决这一问题的最佳方法,例如

/**
 * Returns a "bug free" html string from a jQuery element. Takes care of an IE (>=10) bug which occurs when a textarea has a placeholder attribute.
 *
 * The problem is when a textarea in IE (>=10) has a placeholder attribute, IE also adds the value of this attribute as text/value to the textarea.
 * Which means that .html() of such an element returns something like <textarea placeholder="foo">foo</textarea> (but should be <textarea placeholder="foo"></textarea>),
 * therefore $($target.html()) would return a textarea which now has value (which is the placeholder)!
 */


var tidyHtml = function($target) {
    var cleanHtml = $target.html(), // variable now contains "buggy" textareas
        badIndizes = [];

    $target.find('textarea[placeholder]').each(function(index, element) {
        if($(element).text() !== $(element).val()) { // there is something in .text(), but no value exists! IE bug! (do NOT use .html() here, because it's escaped -> val() is not escaped)
            badIndizes.push(index);
        }
    });

    if(badIndizes.length > 0) {
        cleanHtml = $(cleanHtml).find('textarea[placeholder]').filter(function(index) {
            return $.inArray(index, badIndizes) > -1;
        }).html('').parents().last().wrap('<p>').parent().html();
    }

    return cleanHtml;
};
/**
*从jQuery元素返回“无错误”html字符串。处理当textarea具有占位符属性时发生的IE(>=10)错误。
*
*问题是当IE(>=10)中的textarea具有占位符属性时,IE还会将该属性的值作为文本/值添加到textarea。
*这意味着此类元素的.html()返回类似于foo的内容(但应该是),
*因此,$($target.html())将返回一个现在具有值(即占位符)的textarea!
*/
var tidyHtml=函数($target){
var cleanHtml=$target.html(),//变量现在包含“buggy”文本区域
badIndizes=[];
$target.find('textarea[占位符])。每个(函数(索引,元素){
if($(element).text()!==$(element).val()){//在.text()中有某些内容,但不存在任何值!即bug!(此处不要使用.html(),因为它已转义->val()未转义)
badIndizes.push(索引);
}
});
如果(badIndizes.length>0){
cleanHtml=$(cleanHtml).find('textarea[占位符]).filter(函数(索引){
返回$.inArray(索引,badIndizes)>-1;
}).html(“”).parents().last().wrap(“”).parent().html();
}
返回干净的HTML;
};
信用卡-jamez1414