JavaScript - 使用背景图像加载问题输入占位符文本

发布于 2024-10-19 13:12:03 字数 626 浏览 5 评论 0原文

我有一个输入 type="text" 字段,并尝试用某些默认文本的背景图像替换未得到广泛支持的 HTML5 占位符属性。

在 JavaScript 中,当表单接收焦点时删除背景图像(以便用户在字段中键入的文本不会与背景图像中的文本重叠)并在输入失去焦点时替换背景图像(我正在使用onBlur 事件)。

这工作得很好,除了当用户在输入字段中键入文本,前往另一个页面而不提交此文本并点击浏览器中的返回上一页按钮时,页面会重新加载,但会显示他们未提交的先前文本,仍在背景图像顶部的输入字段中。

我尝试通过检查页面加载时表单是否有价值来解决此问题,如果有则删除背景图像,但这部分对我不起作用。

<input type="text" id="food" onLoad="if(this.value!=''){this.style.backgroundImage='none';}" onFocus="this.style.backgroundImage='none';" onBlur="if(!this.value){this.style.backgroundImage='url(pics/m_form_post.png)';}" name="food"/>

我对可能的解决方案持开放态度。

I have an input type="text" field and am trying to substitute the HTML5 placeholder property that is not widely supported with a background image of some default text.

And in JavaScript, removing the background image when the form receives focus (so that the text the user types in the field does not overlap with the text in the background image) and replacing the background image when the input loses focus (I am using the onBlur event).

This works fine except that when a user types text into the input field, heads to another page without submitting this text and hits the back to previous page button in their browser, the page reloads but with their previous text, that they did not submit, still in the input field on top of the background image.

I tried to fix this by checking if the form has value when the page loads, and if it does then remove the background image but this part isn't working for me.

<input type="text" id="food" onLoad="if(this.value!=''){this.style.backgroundImage='none';}" onFocus="this.style.backgroundImage='none';" onBlur="if(!this.value){this.style.backgroundImage='url(pics/m_form_post.png)';}" name="food"/>

I am open for possible solutions.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

坏尐絯 2024-10-26 13:12:03

我解决了!我只是将 onload 事件放入我的 body 元素而不是 input 元素中,并更改了“this”。到“document.getElementById('食物')”。因为显然 onload 事件仅适用于文档本身(主体元素)和图像。

所以,这就是我的 body 元素的外观:

<body onLoad="if(document.getElementById('food').value!=''){document.getElementById('food').style.backgroundImage='none';}">

这就是我的输入元素的外观:

<input type="text" id="food" onFocus="this.style.backgroundImage='none';" onBlur="if(!this.value){this.style.backgroundImage='url(pics/m_form_post.png)';}" name="food"/>

I solved it! I just put the onload event into my body element instead of the input element and changed the "this." to "document.getElementById('food')." because apparently the onload event only works with the document itself (body element) and images.

So, this is how my body element looks:

<body onLoad="if(document.getElementById('food').value!=''){document.getElementById('food').style.backgroundImage='none';}">

And this is how my input element looks:

<input type="text" id="food" onFocus="this.style.backgroundImage='none';" onBlur="if(!this.value){this.style.backgroundImage='url(pics/m_form_post.png)';}" name="food"/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文