简体   繁体   English

使用jQuery调整窗口大小时更改占位符文本

[英]Changing placeholder text when window resizes with jQuery

With jQuery, how can I change the placeholder text to a textarea when a user is on a small device (~320px and less)? 使用jQuery,当用户在小型设备上时(约320px或更小),如何将占位符文本更改为textarea? I want it to change to "Reply..." for small screens, but anything greater than 320px, it reverts back to "Reply to [name]..." 我希望它更改为“回复...”小屏幕,但任何大于320px,它恢复为“回复[名称] ...”

Currently, my HTML: 目前,我的HTML:

<textarea class="my_textarea" rows="1" type="text" placeholder="Reply to Joe..."></textarea>
<textarea class="my_textarea" rows="1" type="text" placeholder="Reply to Jane..."></textarea>

jQuery: jQuery的:

function changePlaceholder() {
    if( $(window).width() < 320 ){
        $('.my_textarea').attr('placeholder','Reply...');
    } else {
      // how do I change it back here if there are many textarea's on the page?
    }   
}

// initiate first
changePlaceholder();

// resize
$(window).resize( changePlaceholder );

How can I revert back to the original placeholder? 如何恢复原始占位符?

You need to first store all the different placeholders so you can get them back: 您需要先存储所有不同的占位符,以便取回它们:

$('.my_textarea').each(function() {
    $(this).data('placeholder', $(this).attr('placeholder'));
});

function changePlaceholder() {
    if( $(window).width() < 320 ){
        $('.my_textarea').attr('placeholder','Reply...');
    } else {
        $('.my_textarea').each(function() {
            $(this).attr('placeholder', $(this).data('placeholder'));
        });
    }   
}

$(window).resize( changePlaceholder ).trigger('resize');

Multiple issues on your page 您网页上的多个问题

<textarea class="my_textarea"

And your selector is selecting as an id attribute 并且您的选择器正在选择id属性

$('#my_textarea')

Supposed to be 应该是

$('.my_textarea')

You can use $.each to iterate over all the elements and set them accordingly. 您可以使用$.each迭代所有元素并相应地设置它们。

Next , each textarea has different placeholder values. 接下来,每个textarea具有不同的占位符值。 So it is a time for HTML-5 data-* attributes. 所以现在是HTML-5 data- *属性的时候了。

HTML HTML

 <textarea class="my_textarea"
              rows="1" type="text" data-placeholder="Reply to Joe..."
              placeholder="Reply to Joe..."></textarea>

JS JS

function changePlaceholder() {
    var windowWidth = $(window).width();

    if(windowWidth < 320 ) {
        $('.my_textarea').attr('placeholder','Reply...');
    }
    else {
         $('.my_textarea').each(function() {
              var that = this;
              $(this).attr('placeholder', function(_, plc) {
                    return that.data('placeholder');
              });
         });
    }
}

// initiate first
changePlaceholder();

// resize
$(window).resize( changePlaceholder );

Use an array to hold initial values then iterate over array. 使用数组保存初始值,然后迭代数组。

var placeholderValues = [];

function changePlaceholder() {
    if( $(window).width() < 320 ){
        $('.my_textarea').attr('placeholder','Reply...');
    } else {
      $('.my_textarea').each(function(i){
           $(this).attr('placeholder', placeholderValues[i]);
       });
    }   
}

// initiate first
changePlaceholder();

//get values
$('.my_textarea').each(function(){
   placeholderValues.push($(this).attr('placeholder'));
});

// resize
$(window).resize( changePlaceholder );

DEMO: http://jsfiddle.net/dirtyd77/CmaZg/ 演示: http//jsfiddle.net/dirtyd77/CmaZg/

Save the old placeholder in a data attribute, then restore it when returning 将旧占位符保存在数据属性中,然后在返回时将其还原

var shrunk = false; // Keep from repeating this when not necessary
function changePlaceholder() {
    if(!shrunk && $(window).width() < 320){
        $('.my_textarea').each(function() {
            $this = $(this);
            $this.data('save-placeholder', $this.attr('placeholder'))
                 .attr('placeholder','Reply...');
        });
        shrunk = true;
    } else if (shrunk && $(window).width >= 320) {
        $('.my_textarea').each(function() {
            $this = $(this);
            $this.attr('placeholder', $this.data('save-placeholder'));
        });
        shrunk = false;
    }   
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM