获取加载在
Get the link of a page loaded inside a <div> container
容器。
我的jQuery代码是这样的:
$(document).ready(function() { //load the index page into a div container
//set a bottom (border) line under the item of navigation bar
$('#siteloader').load('empleados.jsp');
$('ul#menu li a.active').css({"borderbottom": "4px solid"});
//When the hyperlink is clicked
// set the right color to the item of the navigation bar
$('ul#menu li a').click(function() {
var page = $(this).attr('href');
if (page !== 'index.jsp') {
$('#siteloader').load(page + '.jsp');
$('ul#menu li a').css({"color": "#000"});
$(this).css({"color": "#ca4b00"});
return false;
}
return true;
});
//set the color to the item in which the mouse is hovering ontop
// a bottom (border) line go to the item where i'm hover
$('ul#menu li a').hover(function() {
$('ul#menu li a').css({"color": "#000"});
$('ul#menu li a').css({"border-bottom-style": "none"});
$(this).css({"color": "#ca4b00"});
$(this).css({"border-bottom": "4px solid"});
});
});
这个代码的问题是,如果我不点击一个项目,颜色和底线没有设置为正确的项目。为了将线条和颜色设置到正确的项目,我需要做些什么?
你有两个选择:
- 使用CSS选择器代替javascript添加样式
将
hover
和click
定义为单独的功能,并手动触发它们。$(document).ready(function() { //load the index page into a div container //set a bottom (border) line under the item of navigation bar $('#siteloader').load('empleados.jsp'); $('ul#menu li a.active').css({"borderbottom": "4px solid"}); var onClick = function() { var page = $(this).attr('href'); if (page !== 'index.jsp') { $('#siteloader').load(page + '.jsp'); $('ul#menu li a').css({"color": "#000"}); $(this).css({"color": "#ca4b00"}); return false; } return true; }; var onHover = function() { $('ul#menu li a').css({"color": "#000"}); $('ul#menu li a').css({"border-bottom-style": "none"}); $(this).css({"color": "#ca4b00"}); $(this).css({"border-bottom": "4px solid"}); }; //When the hyperlink is clicked // set the right color to the item of the navigation bar $('ul#menu li a').click(onClick); //set the color to the item in which the mouse is hovering ontop // a bottom (border) line go to the item where i'm hover $('ul#menu li a').hover(onHover); var desiredElement = $('ul#menu li a').eq(0); // the element you want to apply the styles too. Change the `0` value to select other elements. onClick.call(desiredElement); //call the function with the desired element as `this` onHover.call(desiredElement); //call the function with the desired element as `this` });
相关文章:
- 获取ajax加载的图像的大小
- ROR:如何在不重新加载浏览器的情况下从控制器获取参数
- 如何使用Pixijs获取在加载的精灵表资产中定义的图像
- 获取通过AJAX加载的元素
- 用javascript从窗口获取文档,同时检查文档是否已加载
- 加载XML并获取标记.从php更改为javascript
- jQuery:获取图像加载错误的详细信息
- Jquery选择选项并获取只工作一次的更改值(页面加载时)
- 无法获取表单's面板的x和y坐标,并在加载时从cookie中设置它们
- 为什么我在使用 javascript 提交表单时没有通过不加载页面获取帖子数据
- 可靠地将字体加载到 DOM 中以获取画布元素
- 如何使用onLoad加载jQuery JSON.从Zipcode On Form获取城市和州
- 页面加载时通过JQUERY获取SelectList值
- 滚动到底部后获取动态加载的HTML
- 加载后无法获取html内容
- 如何使用cURL获取JavaScript加载的网络内容
- 将从服务器获取的数据加载到输入文本表单中
- Firefox加载项:从选项卡获取文档
- 获取 window.location.reload 以在 Ionic 中仅加载一次
- 如何使用html5中的javascript在页面加载时从查询字符串中获取值