JS&JQuery必备技能

持续更新

1.img

1.1 给img标签赋值src

// jquery
$('#idname').attr('src','https://www.baidu.com/img/baidu_jgylogo3.gif');

1.2 获取img标签src

// jquery
var src = $('#idname')[0].src;

2.location

js

// 2.1 获取当前页面href
var href = window.document.location.href;

// 2.2 获取主机host(不带协议名,如http://)
var host = window.document.location.host;

// 2.3 获取主机名称hostname(不带协议名,如http://)
var hostname = window.document.location.hostname;

// 2.4 获取页面端口
var port = window.document.location.port;

// 2.5获取当前路径
var pathname = window.document.location.pathname;

// 2.6 获取当前主机名称(带协议名与端口)
var currenthostname = window.document.location.href;
currenthostname = currenthostname.substr(0,currenthostname.indexOf(window.document.location.pathname));

3.jquery对象

// id选择器
$('#idname') 
// 类选择器
$('.classname')

jquery对象转换到DOM:
jquery选择器的效果是将整个HTML页面中符合选择条件的所有DOM放入一个集合中,因此对集合使用[0]索引即可取出DOM

// id选择器DOM
$('#idname') [0]
// 类选择器DOM
$('.classname')[0]

猜你喜欢

转载自blog.csdn.net/dongzhensong/article/details/84134011