Javascript 选项卡未正常激活
Javascript tab is not getting active properly
我有三个选项卡,它们的 active/inactive class 来自 javascript 代码。但是当我点击第二个或第三个选项卡时,它会移动到第一个选项卡。查看我的代码
$(document).ready(function () {
$('ul.tabs1').each(function () {
var $active, $content, $links = $(this).find('a');
$active = $($links.filter('[href="' + location.hash + '"]')[0] || $links[0]);
$active.addClass('active');
$content = $($active[0].hash);
$links.not($active).each(function () {
$(this.hash).hide();
});
$(this).on('click', 'a', function (e) {
$active.removeClass('active');
$content.hide();
$active = $(this);
$content = $(this.hash);
$active.addClass('active');
$content.show();
});
});
});
另见 html 相同
<ul class='tabs1'>
<li><a href='#tab1' id="allNews" runat="server" onserverclick="allNews_ServerClick">All News</a></li>
<li><a href='#tab2' id="forNgo" runat="server" onserverclick="forNgo_ServerClick">For NGO</a></li>
<li><a href='#tab3' id="fromNgo" runat="server" onserverclick="fromNgo_ServerClick">From NGO</a></li>
</ul>
每当我单击 second
或 third
选项卡时,它都会再次移动到第一个选项卡。
更新
回发页面加载代码:-
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
HtmlAnchor HA = new HtmlAnchor();
HA.ServerClick += new EventHandler(allNews_ServerClick);
HtmlAnchor HA2 = new HtmlAnchor();
HA2.ServerClick += new EventHandler(forNgo_ServerClick);
HtmlAnchor HA3 = new HtmlAnchor();
HA3.ServerClick += new EventHandler(fromNgo_ServerClick);
BindNews();
}
}
三个标签服务器点击代码:-
protected void ddlSortOrder_SelectedIndexChanged(object sender, EventArgs e)
{
using (SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DefaultCSRConnection"].ConnectionString))
{
//string query = "SELECT dbo.tbl_post.Id, dbo.tbl_post.title, dbo.tbl_post.description, dbo.tbl_post.dateforPost, dbo.tbl_ngoname.ngo_name FROM dbo.tbl_post INNER JOIN dbo.tbl_ngoname ON dbo.tbl_post.NgoId = dbo.tbl_ngoname.Id WHERE DATEPART(YEAR,dbo.tbl_post.dateforPost) = " + ddlYear.SelectedValue + " ORDER BY dbo.tbl_post.dateforPost " + ddlSortOrder.SelectedValue;
string query = "SELECT dbo.tbl_post.Id, dbo.tbl_post.title, dbo.tbl_post.description, dbo.tbl_post.dateforPost, dbo.tbl_ngoname.ngo_name FROM dbo.tbl_post INNER JOIN dbo.tbl_ngoname ON dbo.tbl_post.NgoId = dbo.tbl_ngoname.Id WHERE DATEPART(YEAR,dbo.tbl_post.dateforPost) = " + ddlYear.SelectedValue + " ORDER BY dbo.tbl_post.dateforPost " + ddlSortOrder.SelectedValue;
SqlDataAdapter sda = new SqlDataAdapter(query, conn);
DataTable dt = new DataTable();
sda.Fill(dt);
lstNews.DataSource = dt;
lstNews.DataBind();
}
}
protected void allNews_ServerClick(object sender, EventArgs e)
{
// set user type accordingly in all below methods
using (SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DefaultCSRConnection"].ConnectionString))
{
string query = "SELECT dbo.tbl_post.Id, dbo.tbl_post.title, dbo.tbl_post.description, dbo.tbl_post.dateforPost, dbo.tbl_ngoname.ngo_name, dbo.tbl_User.usertype FROM dbo.tbl_post INNER JOIN dbo.tbl_ngoname ON dbo.tbl_post.NgoId = dbo.tbl_ngoname.Id INNER JOIN dbo.tbl_User ON dbo.tbl_post.userId = dbo.tbl_User.Id where usertype != '2'";
SqlDataAdapter sda = new SqlDataAdapter(query, conn);
DataTable dt = new DataTable();
sda.Fill(dt);
lstNews.DataSource = dt;
lstNews.DataBind();
allNews.Attributes["class"] = "active";
}
}
protected void forNgo_ServerClick(object sender, EventArgs e)
{
using (SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DefaultCSRConnection"].ConnectionString))
{
string query = "SELECT dbo.tbl_post.Id, dbo.tbl_post.title, dbo.tbl_post.description, dbo.tbl_post.dateforPost, dbo.tbl_ngoname.ngo_name, dbo.tbl_User.usertype FROM dbo.tbl_post INNER JOIN dbo.tbl_ngoname ON dbo.tbl_post.NgoId = dbo.tbl_ngoname.Id INNER JOIN dbo.tbl_User ON dbo.tbl_post.userId = dbo.tbl_User.Id where usertype != '2'";
SqlDataAdapter sda = new SqlDataAdapter(query, conn);
DataTable dt = new DataTable();
sda.Fill(dt);
lstNews.DataSource = dt;
lstNews.DataBind();
forNgo.Attributes["class"] = "active";
}
}
protected void fromNgo_ServerClick(object sender, EventArgs e)
{
using (SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DefaultCSRConnection"].ConnectionString))
{
string query = "SELECT dbo.tbl_post.Id, dbo.tbl_post.title, dbo.tbl_post.description, dbo.tbl_post.dateforPost, dbo.tbl_ngoname.ngo_name, dbo.tbl_User.usertype FROM dbo.tbl_post INNER JOIN dbo.tbl_ngoname ON dbo.tbl_post.NgoId = dbo.tbl_ngoname.Id INNER JOIN dbo.tbl_User ON dbo.tbl_post.userId = dbo.tbl_User.Id where usertype = '2'";
SqlDataAdapter sda = new SqlDataAdapter(query, conn);
DataTable dt = new DataTable();
sda.Fill(dt);
lstNews.DataSource = dt;
lstNews.DataBind();
fromNgo.Attributes["class"] = "active";
}
}
我不是 100% 确定,但我认为你应该参考 e.target
而不是 this
:
$(document).ready(function () {
$('ul.tabs1').each(function () {
var $active, $content, $links = $(this).find('a');
$active = $($links.filter('[href="' + location.hash + '"]')[0] || $links[0]);
$active.addClass('active');
$content = $($active[0].hash);
$links.not($active).each(function () {
$(this.hash).hide();
});
$(this).on('click', 'a', function (e) {
e.preventDefault();
$active.removeClass('active');
$content.hide();
$active = $(e.target);
$content = $(location.hash);
$active.addClass('active');
$content.show();
});
});
});
您还应该使用 e.preventDefault()
来停止默认的点击事件。就风格而言,我不建议使用 $ 作为变量,太接近 jQuery $ 用法而不是惯例。
我有三个选项卡,它们的 active/inactive class 来自 javascript 代码。但是当我点击第二个或第三个选项卡时,它会移动到第一个选项卡。查看我的代码
$(document).ready(function () {
$('ul.tabs1').each(function () {
var $active, $content, $links = $(this).find('a');
$active = $($links.filter('[href="' + location.hash + '"]')[0] || $links[0]);
$active.addClass('active');
$content = $($active[0].hash);
$links.not($active).each(function () {
$(this.hash).hide();
});
$(this).on('click', 'a', function (e) {
$active.removeClass('active');
$content.hide();
$active = $(this);
$content = $(this.hash);
$active.addClass('active');
$content.show();
});
});
});
另见 html 相同
<ul class='tabs1'>
<li><a href='#tab1' id="allNews" runat="server" onserverclick="allNews_ServerClick">All News</a></li>
<li><a href='#tab2' id="forNgo" runat="server" onserverclick="forNgo_ServerClick">For NGO</a></li>
<li><a href='#tab3' id="fromNgo" runat="server" onserverclick="fromNgo_ServerClick">From NGO</a></li>
</ul>
每当我单击 second
或 third
选项卡时,它都会再次移动到第一个选项卡。
更新
回发页面加载代码:-
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
HtmlAnchor HA = new HtmlAnchor();
HA.ServerClick += new EventHandler(allNews_ServerClick);
HtmlAnchor HA2 = new HtmlAnchor();
HA2.ServerClick += new EventHandler(forNgo_ServerClick);
HtmlAnchor HA3 = new HtmlAnchor();
HA3.ServerClick += new EventHandler(fromNgo_ServerClick);
BindNews();
}
}
三个标签服务器点击代码:-
protected void ddlSortOrder_SelectedIndexChanged(object sender, EventArgs e)
{
using (SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DefaultCSRConnection"].ConnectionString))
{
//string query = "SELECT dbo.tbl_post.Id, dbo.tbl_post.title, dbo.tbl_post.description, dbo.tbl_post.dateforPost, dbo.tbl_ngoname.ngo_name FROM dbo.tbl_post INNER JOIN dbo.tbl_ngoname ON dbo.tbl_post.NgoId = dbo.tbl_ngoname.Id WHERE DATEPART(YEAR,dbo.tbl_post.dateforPost) = " + ddlYear.SelectedValue + " ORDER BY dbo.tbl_post.dateforPost " + ddlSortOrder.SelectedValue;
string query = "SELECT dbo.tbl_post.Id, dbo.tbl_post.title, dbo.tbl_post.description, dbo.tbl_post.dateforPost, dbo.tbl_ngoname.ngo_name FROM dbo.tbl_post INNER JOIN dbo.tbl_ngoname ON dbo.tbl_post.NgoId = dbo.tbl_ngoname.Id WHERE DATEPART(YEAR,dbo.tbl_post.dateforPost) = " + ddlYear.SelectedValue + " ORDER BY dbo.tbl_post.dateforPost " + ddlSortOrder.SelectedValue;
SqlDataAdapter sda = new SqlDataAdapter(query, conn);
DataTable dt = new DataTable();
sda.Fill(dt);
lstNews.DataSource = dt;
lstNews.DataBind();
}
}
protected void allNews_ServerClick(object sender, EventArgs e)
{
// set user type accordingly in all below methods
using (SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DefaultCSRConnection"].ConnectionString))
{
string query = "SELECT dbo.tbl_post.Id, dbo.tbl_post.title, dbo.tbl_post.description, dbo.tbl_post.dateforPost, dbo.tbl_ngoname.ngo_name, dbo.tbl_User.usertype FROM dbo.tbl_post INNER JOIN dbo.tbl_ngoname ON dbo.tbl_post.NgoId = dbo.tbl_ngoname.Id INNER JOIN dbo.tbl_User ON dbo.tbl_post.userId = dbo.tbl_User.Id where usertype != '2'";
SqlDataAdapter sda = new SqlDataAdapter(query, conn);
DataTable dt = new DataTable();
sda.Fill(dt);
lstNews.DataSource = dt;
lstNews.DataBind();
allNews.Attributes["class"] = "active";
}
}
protected void forNgo_ServerClick(object sender, EventArgs e)
{
using (SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DefaultCSRConnection"].ConnectionString))
{
string query = "SELECT dbo.tbl_post.Id, dbo.tbl_post.title, dbo.tbl_post.description, dbo.tbl_post.dateforPost, dbo.tbl_ngoname.ngo_name, dbo.tbl_User.usertype FROM dbo.tbl_post INNER JOIN dbo.tbl_ngoname ON dbo.tbl_post.NgoId = dbo.tbl_ngoname.Id INNER JOIN dbo.tbl_User ON dbo.tbl_post.userId = dbo.tbl_User.Id where usertype != '2'";
SqlDataAdapter sda = new SqlDataAdapter(query, conn);
DataTable dt = new DataTable();
sda.Fill(dt);
lstNews.DataSource = dt;
lstNews.DataBind();
forNgo.Attributes["class"] = "active";
}
}
protected void fromNgo_ServerClick(object sender, EventArgs e)
{
using (SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DefaultCSRConnection"].ConnectionString))
{
string query = "SELECT dbo.tbl_post.Id, dbo.tbl_post.title, dbo.tbl_post.description, dbo.tbl_post.dateforPost, dbo.tbl_ngoname.ngo_name, dbo.tbl_User.usertype FROM dbo.tbl_post INNER JOIN dbo.tbl_ngoname ON dbo.tbl_post.NgoId = dbo.tbl_ngoname.Id INNER JOIN dbo.tbl_User ON dbo.tbl_post.userId = dbo.tbl_User.Id where usertype = '2'";
SqlDataAdapter sda = new SqlDataAdapter(query, conn);
DataTable dt = new DataTable();
sda.Fill(dt);
lstNews.DataSource = dt;
lstNews.DataBind();
fromNgo.Attributes["class"] = "active";
}
}
我不是 100% 确定,但我认为你应该参考 e.target
而不是 this
:
$(document).ready(function () {
$('ul.tabs1').each(function () {
var $active, $content, $links = $(this).find('a');
$active = $($links.filter('[href="' + location.hash + '"]')[0] || $links[0]);
$active.addClass('active');
$content = $($active[0].hash);
$links.not($active).each(function () {
$(this.hash).hide();
});
$(this).on('click', 'a', function (e) {
e.preventDefault();
$active.removeClass('active');
$content.hide();
$active = $(e.target);
$content = $(location.hash);
$active.addClass('active');
$content.show();
});
});
});
您还应该使用 e.preventDefault()
来停止默认的点击事件。就风格而言,我不建议使用 $ 作为变量,太接近 jQuery $ 用法而不是惯例。