• jQuery实现搜索框插件
  • 豆瓣音乐接口实现豆瓣搜索框

豆瓣接口有时不稳定,网络请求会报400,不要惊慌。我主要是练习一下jQuery的JSONP和封装插件。

<div class="searchWrapper">

</div>
<script src="../js/searchBox.js"></script>
<script>
$(".searchWrapper").search({
placeholder:'唱片名、表演者、条码、ISRC',
url:'https://api.douban.com/v2/music/search',
type:'GET',
dataType:'jsonp',
sucFn:callbackObj.doubanCa,
// data:'tag=',
data:'q=',
count:'&count=7',
errFn:function(){
console.log('error');
}
});
//如果鼠标点击了除搜索框以外的地方,就隐藏下拉列表
$(document).on("mousedown",function(e){
var event = e || window.event;
var target = event.target;
if(target != $('.searchText')[0] && $(target).parents(".searchList")){
$('.searchList').hide();
}
});
formObj.doubanForm();
</script>

css代码参考就好:

 /*
* 样式没有做分类处理采用注释提示
* 建议不修改项:表示保留插件样式风格
* 建议保留项:表示会影响插件样式结构,不能随意修改
*/
*{
padding: 0px;
margin: 0px;
list-style: none;
}
.searchBox{
border: none;/*建议不修改项*/
width: 460px;
height: 30px;
margin-top: 15px;
border-top-left-radius: 5px;/*建议不修改项*/
border-bottom-left-radius: 5px;/*建议不修改项*/
border-top-right-radius: 5px;/*建议不修改项*/
border-bottom-right-radius: 5px;/*建议不修改项*/
border-bottom: 1px solid #bbb;/*建议不修改项*/
overflow: hidden;/*建议不修改项
}
.searchBox::after{/*建议保留项*/
content: "";
clear: both;
display: block;
}
.searchBox .searchText{
float: left;/*建议保留项*/
padding-left: 5px;
width: 415px;
height: 30px;
line-height: 30px;
font-size: 13px;
outline: none;/*建议不修改项*/
border-style: none;/*建议不修改项*/ }
.searchBox .searchButton{
display: block;
width: 40px;
height: 30px;
outline: none;
border-style: none;/*建议不修改项*/
position: relative;
z-index:;
opacity:;
}
.searchList{
position: absolute;/*建议保留项*/
/* display: none; */
width: 425px;
border-left: 1px solid #87CEEB;
border-right: 1px solid #87CEEB;
}
.searchList li{
width: 420px;
padding-left: 5px;
padding-top: 5px;
padding-bottom: 5px;
border-bottom: 1px solid #87CEEB;
background-color: #fff;
}
.searchList li:hover{
background-color: #f0f3ef;
}
.searchList li a{
display: block;
width: 100%;
height: 100%;
}
.searchList li a::after{
content: "";
clear: both;
display: block;
}
.searchList li::after{
content: "";
clear: both;
display: block;
}
.searchList img,div{
float: left;
width: 48px;
}
.searchList img{
float: left;
width: 48px;
}
.searchList div{
float: left;
width: 372px;
}
.searchList em{
padding-left: 5px;
font-size: 14px;
font-style: normal;
color: #ff00ff;
}
.searchList span{
font-size: 12px;
color: #888;
} /* 搜索按钮图标 */
.searchBuDivf{
position: relative;
width: 40px;
height: 30px;
background-color:#bbb;/*建议不修改项*/
}
.searchBuDivf::after{
content: "";
clear: both;
display: block;
}
.searchBuDiv{
float: left;
width: 12px;
height: 12px;
position: relative;
border-radius: 100%;
border:2px solid #fff;
position: relative;
margin-top: -25px;
margin-left: 9px;
}
.searchBuDiv::after{
content: "";
-webkit-transform: rotate(45deg);
-moz-transform: rotate(45deg);
transform: rotate(45deg);
width:8px;
height: 2px;
position: absolute;
top:13px;
left:11px;
background-color: #fff;
}

js代码:

 (function($){
//搜索框插件对象
var searchBoxObj = {
init:function(options){
this.opt = options || {};//将必要的参数缓存到插件对象上,并且使用空对象避免无参数时报错
this.creatDom();//生成DOM结构
this.bindEvent();//给输入框绑定input事件
},
creatDom:function(){//生成DOM结构的方法
var htmlStr = '<form class="searchForm" action="" target="_blank" autocomplete="off">\
<fieldset class="searchBox">\
<legend></legend>\
<input type="text" name="" class="searchText">\
<div class="searchBuDivf">\
<input type="submit" name="" class="searchButton" value="" />\
<div class="searchBuDiv"></div>\
</div>\
</fieldset>\
<ul class="searchList"></ul>\
</form>';
this.opt.father.html(htmlStr);//将插件结构插入到插件容器
$('.searchText').attr("placeholder",this.opt.placeholder); },
bindEvent:function(){//实现输入框input事件绑定
var self = this;
$('.searchText').on('input',function(e){
e.preventDefault();//阻止默认事件
var value = $(this).val();
self.getData(value);//发送请求,获取数据
});
},
getData:function(val){
//根据构建数据构建ajax数据请求方法
var self = this;
var opt = self.opt;
$.ajax({
type:opt.type,
url:opt.url,
dataType:opt.dataType,
jsonp:opt.jsonp,
data:opt.data + val + opt.count,
success:function(data){
opt.sucFn(data);
},
error:function(){
opt.errFn();
}
})
}
}
//在jQuery上扩展插件方法
$.fn.extend({
//options为定义插件的一些数据
search:function(options){
options.father = this || $('body');//将插件容器缓存在options上
searchBoxObj.init(options);//调用插件生成器
}
});
})(jQuery);
//
var callbackObj = {
//豆瓣音乐接口跨域请求成功的回调函数
doubanCa:function(data){
var list = data.musics;
console.log(list);
var $searchList = $('.searchList');
var str = '';
for(var i = 0; i < list.length; i++){
str += '<li>\
<a href="' + list[i].alt +'" onclick="">\
<img src="' + list[i].image + '">\
<div>\
<em>' + list[i].title + '</em>\
<span>(' + list[i].author[0].name + ')</span>\
</div>\
</a>\
</li>';
}
$searchList.html($(str));
$searchList.show();
}
}
var formObj = {
doubanForm:function(){
$('.searchForm').attr('action','https://music.douban.com/subject_search');
$('.searchText').attr('name','search_text');
}
} // <li>
// <a href="https://site.douban.com/adele/" onclick="">
// <img src="https://img3.doubanio.com/view/site/small/public/71c3285636cc0af.jpg" alt="">
// <div>
// <em>adele</em>
// <span>(音乐人)</span>
// </div>
// </a>
// </li>
// 豆瓣API 开发平台:
// https://developers.douban.com/wiki/?title=guide
// 豆瓣API V2 (测试版):
// https://developers.douban.com/wiki/?title=api_v2
// {
// placeholder:'唱片名、表演者、条码、ISRC',
// url:'https://api.douban.com/v2/music/search',
// type:'GET',
// dataType:'jsonp',
// sucFn:function(data){
// console.log(data);
// },
// data:'q=',//搜索的关键字
// count:'&count=7',//搜索数据的条数
// errFn:function(){
// console.log('error');
// }
// } //百度search(options)中的options参数
// {
// placeholder:'请输入关键字',
// url:'https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su',
// type:'GET',
// dataType:'jsonp',
// sucFn:function(data){
// console.log(data);
// },
// data:'wd=',
// jsonp:'cb',
// count:'',
// errFn:function(){
// console.log('error');
// }
// }

jQuery实现搜索框插件+豆瓣音乐接口实现豆瓣搜索框的更多相关文章

  1. jQuery打造智能提示插件二(可编辑下拉框)

    在上一篇 jQuery打造智能提示插件 上改进,增加下拉按钮,修复点击下拉区域外不隐藏BUG 效果 下拉按钮素材: js封装,注意红色部分为BUG修复,然后传入boxwidth不带px: /* /// ...

  2. 弹出框插件layer使用

    layer是一款近年来备受青睐的web弹层组件,她具备全方位的解决方案,致力于服务各水平段的开发人员,您的页面会轻松地拥有丰富友好的操作体验. 插件官方地址:http://layer.layui.co ...

  3. Dropdown.js基于jQuery开发的轻量级下拉框插件

    Dropdown.js 前言 在SPA(Single Page Application)盛行的时代,jQuery插件的轮子正在减少,由于我厂有需求而开发了这个插件.如果觉得本文对您有帮助,请给个赞,以 ...

  4. 非常强大的jQuery万能浮动框插件

    支持hover, click, focus以及无事件触发:支持多达12种位置的定位,出界自动调整:支持页面元素加载,Ajax加载,下拉列表,提示层效果,tip类效果等:可自定义装载容器:内置UI不错的 ...

  5. jQuery自定义漂亮的下拉框插件8种效果演示

    原始的下拉框不好看这里推荐一个jQuery自定义漂亮的下拉框插件8种效果演示 在线预览 下载地址 实例代码 <!DOCTYPE html> <html lang="en&q ...

  6. FancySelect – 更好用的 jQuery 下拉选择框插件

    FancySelect 这款插件是 Web 开发中下拉框功能的一个更好的选择.FancySelect 使用方便,只要绑定页面上的任何 Select 元素,并调用就 .fancySelect() 就可以 ...

  7. [原]发布一个jQuery提示框插件,Github开源附主站,jquery.tooltips.js

    一个简单精致的jQuery带箭头提示框插件 插件写好快一年了,和一个 弹出框插件(点击查看) 一起写的,一直没有整理出来,昨天得功夫整理并放到了github上,源码和网站均可在线看或下载. CSS中的 ...

  8. jQuery下拉框插件8种效果

    jQuery自定义漂亮的下拉框插件8种效果 jquery美化选择器实例有:边框.下划线. 伸缩 .滑动. 覆盖. 旋转. 弹出层选择 .环形效果. 在线预览 <body class=" ...

  9. jquery仿alert提示框、confirm确认对话框、prompt带输入的提示框插件[附实例演示]

    jquery仿alert提示框.confirm确认对话框.prompt带输入的提示框插件实例演示 第一步:引入所需要的jquery插件文件: http://www.angelweb.cn/Inc/eg ...

随机推荐

  1. jar包 pom

    动态的web工程tomcat 自带jar包: jstl: taglibs-standard-impl-1.2.5.jar taglibs-standard-spec-1.2.5.jar   //以下是 ...

  2. 关于LeetCode上链表题目的一些trick

    最近在刷leetcode上关于链表的一些高频题,在写代码的过程中总结了链表的一些解题技巧和常见题型. 结点的删除 指定链表中的某个结点,将其从链表中删除. 由于在链表中删除某个结点需要找到该结点的前一 ...

  3. nginx预防常见攻击

    目录 nginx防止DDOS攻击 概述 攻击手段 配置 限制请求率 限制连接的数量 关闭慢连接 设置 IP 黑名单 设置IP白名单 小站点解决方案 nginx防止CC攻击 概述 主动抑制方法 应用举例 ...

  4. [idea] SpringBoot整合swagger2实现CRUD

    一:创建SpringBoot ,在pom.xml文件中加入jar包 <dependency> <groupId>io.springfox</groupId> < ...

  5. mac上修改host

    host文件下载地址: https://github.com/highsea/Hosts/blob/master/hosts https://github.com/racaljk/hosts 备份ma ...

  6. oracle 关于对时间操作的汇总

    -- 对时间的操作 对当前日期增加一个小时: SQL> select sysdate, sysdate+numtodsinterval(1,’hour’) from dual ; 对当前日期增加 ...

  7. java中的超类是什么

    超类(SuperClass) :用java术语来讲,被继承的类称为超类(SuperClass),也有叫做父类,继承的类称为子类.

  8. 课堂练习6--统计txt文本

    统计文本中26个字母的频率: package bao; import java.io.BufferedReader; import java.io.FileReader; import java.io ...

  9. MySQL操作(备份很重要)

    文档一: --修改用户密码的命令 mysqladmin -uroot -proot123 password mysql123 --登录mysql数据库的命令 mysql -uroot -proot12 ...

  10. Echarts学习之路3(在react中使用)

    安装: npm install echarts --save demo import React, { Component } from 'react'; // 引入 ECharts 主模块 impo ...