【问题标题】:declaring div in c# with onclick event在 C# 中使用 onclick 事件声明 div
【发布时间】:2009-10-02 20:33:46
【问题描述】:

我正在尝试在 c# 代码隐藏中创建一个 div 容器。我希望 div 容器在单击后引发一个 javascript 事件 onclick。它工作正常。下面是调用 javascript 函数“ds()”的代码参数。问题是我想将参数传递给javascript函数。我不知道如何完成它。请帮助我。

//string userIcon = "gbn";

sb.Append(userIcon + @"<div onclick=""ds()"" style='background-color: #EFEFEF; padding: 10px;'>" + "<a href=#>" + "MubbshirAbbs" + "</a></div><br>");

Literal1.Text = sb.ToString();

Javascript 函数是这样的:

function ds()
{
  var a="";alert("123");
  document.getElementById("TextBox1").style.backgroundColor="red";
}

请告诉我如何在 div onclick 函数中添加参数。

【问题讨论】:

    标签: c# asp.net javascript html


    【解决方案1】:

    修改 C# 代码:

    sb.Append(userIcon);
    sb.Append(@"<div onclick=""ds(");
    sb.Append("'clicked'");
    sb.Append(@")"" style='background-color: #EFEFEF; padding: 10px;'>");
    sb.Append("<a href=#>");
    sb.Append("MubbshirAbbs");
    sb.Append("</a></div><br>");
    

    修改 javascript:

    function ds(param)
    {
      var a="";alert("123");
      document.getElementById("TextBox1").style.backgroundColor="red";
    }
    

    【讨论】:

      【解决方案2】:

      我不知道我有没有遇到你的问题,你不能这样做吗?:

      sb.Append(userIcon + @"<div onclick=""ds(" + parameter + ")"" style='background-color: #EFEFEF; padding: 10px;'>" + "<a href=#>" + "MubbshirAbbs" + "</a></div><br>");
      Literal1.Text = sb.ToString();
      function ds(parameter)
      {
      var a="";alert(parameter);
      document.getElementById("TextBox1").style.backgroundColor="red";
      }
      

      【讨论】:

        猜你喜欢
        • 2013-03-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-02-07
        • 2016-08-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多