共计 2060 个字符,预计需要花费 6 分钟才能阅读完成。
jQuery 代码片段,用于获取当前网页的完整标题并将其存储在变量中以供其他脚本使用。这是您在浏览器标题上看到的标题。
不多说,直接上代码
当前页面标题,链接
var current_href = $(location).attr('href');
var current_title = $(document).attr('title');
获取当前焦点的元素
用于获取具有当前焦点的元素并在失去焦点后执行 AJAX 自动保存的 jQuery 代码片段。
//change focus when tabbed across into new item
$('.item :input').live('focus', function()
{var thisItem = $(this).parents('.item');
var thisId = thisItem.attr('id');
$('.item').removeClass('item-selected'); //remove all focus
thisItem.addClass('item-selected'); //add focus to current item
//save those items that have changed
$.each($('.item-changed'), function(i,v)
{var currItemId = $(this).attr('id');
//exclude current item
if (!_this.helpers.itemHasFocus(currItemId))
{console.log('saving'+currItemId);
_this.save.item(currItemId); /* AJAX auto save */
$(this).removeClass('item-changed');
}
});
});
jQuery 获取 URL 参数
jQuery 代码片段,用于获取作为参数存储在 url 中的动态变量,并将它们存储为 JavaScript 变量,以供您的脚本使用。随着世界转向动态 Web 应用程序,其使用方式与哈希 URL 不同。因此,诸如解码 URL 字符串之类的东西将在未来几年内变得越来越流行。
$.urlParam = function(name){var results = new RegExp('[?&]' + name + '=([^]*)').exec(window.location.href);
return results[1] || 0;
}
// example.com?param1=name¶m2=&id=6
$.urlParam('param1'); // name
$.urlParam('id'); // 6
$.urlParam('param2'); // null
//example params with spaces
http://www.jquery4u.com?city=Gold Coast
console.log($.urlParam('city'));
//output: Gold%20Coast
console.log(decodeURIComponent($.urlParam('city')));
//output: Gold Coast
例如,这可以用于设置文本输入字段的默认值:
$('#city').val(decodeURIComponent($.urlParam('city')));
感谢 网友 对此功能的改进: 文章来源:https://www.toymoban.com/diary/js/425.html
$.urlParam = function(name){var results = new RegExp('[?&]' + name + '=([^]*)').exec(window.location.href);
if (results==null){return null;}
else{return results[1] || 0;
}
}
文章来源地址 https://www.toymoban.com/diary/js/425.html
到此这篇关于 jQuery 各种获取信息,如当前页面标题, 链接,当前焦点的元素的文章就介绍到这了, 更多相关内容可以在右上角搜索或继续浏览下面的相关文章,希望大家以后多多支持 TOY 模板网!
原文地址:https://www.toymoban.com/diary/js/425.html
如若转载,请注明出处:如若内容造成侵权 / 违法违规 / 事实不符,请联系站长进行投诉反馈,一经查实,立即删除!
正文完