Asp.Net_后台代码访问前台html标签

时间:2021-02-24 03:35:58
//单击按钮后批量改变li元素的内联文本值及样式
for (int i = ; i <= ; i++)
{
HtmlGenericControl li = this.FindControl("li" + i) as HtmlGenericControl;
li.InnerHtml = "新值" + i.ToString();
li.Attributes.CssStyle.Value = "color:red";
}

Code

//单击按钮后批量改变li元素的内联文本值及样式
foreach (Control control in ul1.Controls)
{
if (control is HtmlGenericControl)
{
HtmlGenericControl li = control as HtmlGenericControl;
li.InnerHtml = "新值" + (counter++).ToString();
li.Attributes.CssStyle.Value = "color:red";
}
}

Code

//单击按钮后批量改变li元素的内联文本值及样式
HtmlDocument htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(ul1.InnerHtml);
HtmlNodeCollection lis = htmlDoc.DocumentNode.SelectNodes("li");
for (int i = ; i < lis.Count; i++)
{
lis[i].InnerHtml = "新值" + (i + ).ToString();
lis[i].Attributes.Add("style", "color:red");
}
ul1.InnerHtml = htmlDoc.DocumentNode.InnerHtml;

Code