【问题标题】:Getting the id of the div on button click where div contains that button [duplicate]在按钮单击时获取 div 的 id,其中 div 包含该按钮 [重复]
【发布时间】:2019-07-16 12:14:01
【问题描述】:

我目前正在建立一个网站,但在单击 div 按钮包含按钮的位置获取按钮上的 div id 时遇到问题。

<div id="div" class="tab-content" title="Foods">
  <div class="container">
    <div class="row">
      <div class="col-lg-2">
        <div class="tabs-block">
          <asp:Button ID="btnSend" runat="server" OnClick=""/>
         </div>
      </div>
    </div>
   </div>
  </div>
 </div>

这是不是我的代码但类似的代码。 我正在尝试的是,当我单击 btnSend 按钮时,我需要获取选项卡内容 ID 和标题“食物”,我尝试使用 javascript 和 jquery 获取标题,然后我将在我的后台代码是

 protected void btnSend_Click(object sender, EventArgs e)
        {
            try
            {
                string message = "";
                message += "<p>"Title here" Resevation Form </p><br />";
            {
        }

我需要在后台代码中使用该标题,即“此处的标题”

【问题讨论】:

  • 如果你使用 jquery 试试 btnSend_Click 函数:$(sender).parent().parent().parent().parent().parent().attr("id")获取 id 和 $(sender).parent().parent().parent().parent().parent().attr("title") 以获取标题。这可能会对您有所帮助。
  • @ParthPatel OP 没有说任何关于 jQuery 的使用...如果 jQuery 被接受,那么使用 closest() 会比很多 parents() 更好的主意
  • 用 Jquery 使用 closest 方法 $(this).closest('.tab-content'); 你想使用纯 JavaScript 吗?
  • JavaScript 有这样的解决方案(请注意本示例中不支持 IE)...stackoverflow.com/questions/22119673/…

标签: javascript c# html css


【解决方案1】:

你应该这样写:

<div class="col-lg-2">
    <div class="tabs-block" name="Title">
      <Button ID="btnSend" runat="server"/>
     </div>
  </div>

$( "#btnSend" ).click(function(event) {
  console.log($(this).parent().attr("name"))
});

在 $(this).parent().attr("name") 中,您将拥有所单击选项卡的名称。

【讨论】:

  • OP没有使用或没有提到使用jQuery,显然他是在使用asp.net和C#代码来制作按钮点击事件。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-01-25
  • 1970-01-01
  • 2017-04-12
  • 1970-01-01
  • 1970-01-01
  • 2014-12-22
  • 1970-01-01
相关资源
最近更新 更多