原生 input 记录

文章介绍了如何使用CSS伪类(::-webkit-input-placeholder,::-moz-placeholder等)改变输入框placeholder的颜色,并在输入框聚焦时去除边框,同时处理不同浏览器的兼容性问题。着重讲解了:focus状态下的边框处理和outline的使用。
摘要由CSDN通过智能技术生成
  1. 改变 placeholder 颜色
    ::-webkit-input-placeholder {  /* WebKit, Blink, Edge */
      color: #f00;
    }
    :-moz-placeholder {  /* Mozilla Firefox 4 to 18 */
      color: #f00;
      opacity: 1;
    }
    ::-moz-placeholder {  /* Mozilla Firefox 19+ */
      color: #f00;
      opacity: 1;
    }
    :-ms-input-placeholder {  /* Internet Explorer 10-11 */
      color: #f00;
    }
    
  2. 输入框聚焦时,去除边框
    input边框使用border修改样式,但是聚焦高亮时修改border 没有效果,需要使用 outline
    <input class="ipt1" type="text" value="" placeholder="请输入" />
    
    <style>
      .ipt1 {
    	max-width: 10rem;
    	height: 1.56rem;
    	font-weight: 600;
    	font-size: 1.13rem;
    	color: #0b8cbf;
    	line-height: 1.56rem;
    	text-align: end;
    	border: none;
    	&:focus {
    		outline: 0;
    		border: none;
    	}
    }
    </style>
    
可以的,可以通过保存历史搜索记录的方式,在用户点击历史搜索记录时,将历史搜索记录中的文本填入搜索框中,然后再通过代码触发搜索操作。下面是一个简单的示例代码: ```html <!-- 搜索表单 --> <form id="search-form"> <input type="text" id="search-input" placeholder="搜索..."> <button type="submit">搜索</button> </form> <!-- 历史搜索记录列表 --> <ul id="search-history"> <li>搜索记录1</li> <li>搜索记录2</li> <li>搜索记录3</li> </ul> <script> // 获取搜索表单和历史搜索记录列表 const searchForm = document.getElementById('search-form'); const searchInput = document.getElementById('search-input'); const searchHistory = document.getElementById('search-history'); // 从本地存储中获取历史搜索记录 const history = JSON.parse(localStorage.getItem('search_history')) || []; // 显示历史搜索记录 history.forEach(item => { const li = document.createElement('li'); li.textContent = item; searchHistory.appendChild(li); }); // 点击历史搜索记录时,填入搜索框并搜索 searchHistory.addEventListener('click', event => { const target = event.target; if (target.nodeName === 'LI') { searchInput.value = target.textContent; searchForm.submit(); // 触发搜索操作 } }); // 提交搜索表单时,保存搜索记录到本地存储 searchForm.addEventListener('submit', event => { event.preventDefault(); const keyword = searchInput.value.trim(); if (keyword) { // 将搜索关键字添加到历史搜索记录中 history.unshift(keyword); // 最多保存10条历史搜索记录 history.splice(10); // 将历史搜索记录保存到本地存储中 localStorage.setItem('search_history', JSON.stringify(history)); // 触发搜索操作 // ... } }); </script> ``` 这段代码实现了一个简单的搜索功能,包括保存历史搜索记录和点击历史搜索记录进行搜索的功能。你可以根据自己的需求进行修改和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值