如何传递<a> tag to <div> tag when its shown using jquery?</div></a>的名称

时间:2015-03-02 07:06:50

标签: javascript jquery html

在我的表单中,当他们点击我的div标签的内容时,使用jQuery显示了4个链接。

我的问题是,点击链接后,我想将该链接的名称传递给该div代码,该怎么办?

&#13;
&#13;
            $("#adduser").click(function() {
              $("#add").toggle("slow");
            });
&#13;
<div class="box-content" id="adduser">
  <a class="quick-button span2" href="#">
    <i class="icon-group"></i>
    <p>Add Mentors</p>
    <span class="notification green">167</span>
  </a>
  <a class="quick-button span2" href="#">
    <i class="icon-group"></i>
    <p>Add Students</p>
  </a>
  <a class="quick-button span2" href="#">
    <i class="icon-group"></i>
    <p>Add Faculties</p>
  </a>
  <a class="quick-button span2" href="#">
    <i class="icon-group"></i>
    <p>Add Faculty Advisors</p>
  </a>
</div>


<div class="row-fluid sortable " id="add" style="display: none">
  <div class="box span12">
    <div class="box-header" data-original-title>

      <h2><i class="halflings-icon white edit"></i><span class="break"></span>Add new User</h2>
      <div class="box-icon">
        <a href="#" class="btn-minimize"><i class="halflings-icon white chevron-up"></i></a>
        <a href="#" class="btn-close"><i class="halflings-icon white remove"></i></a>
      </div>
    </div>
    <div class="box-content">
      <form enctype='multipart/form-data' method="post" class="form-horizontal" action="">
        <div class="control-group">
          <label class="control-label" for="fileInput">Select file</label>
          <div class="controls">
            <input class="input-file uniform_on" id="file" name="file" type="file">
            <input type="submit" class="blue btn" value="Add">

          </div>
          <br>
          <div class="controls">
            <a href="">Or add single user</a>
          </div>
        </div>

      </form>
    </div>
  </div>
</div>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:0)

我不确定你是否在寻找这个。你能检查..

$("a").click(function(){
     // Get Link Name Attribute
    var LinkName=$(this).attr("name");
    //Get Link Text/Value
    var LinkText=$(this).text();
    
    //Display for demo
    alert(LinkName+'\n'+LinkText);
  
    //Display LinkText in SomeDiv
    $('#SomeDiv').text(LinkText);
    
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="#" name="Anchor Name">Link Text</a><hr/>

<div id="SomeDiv"></div>

答案 1 :(得分:0)

我刚刚更改了标题以打印出按下的链接名称。

希望有帮助

$(function() {
	$(".quick-button").click(function(event) {
		$("h2").text($(event.target).text());		
		$("#add").toggle("slow");
	}); 
});
<div class="box-content" id="adduser">
  <a class="quick-button span2" href="#">
    <i class="icon-group"></i>
    <p>Add Mentors</p>
    <span class="notification green">167</span>
  </a>
  <a class="quick-button span2" href="#">
    <i class="icon-group"></i>
    <p>Add Students</p>
  </a>
  <a class="quick-button span2" href="#">
    <i class="icon-group"></i>
    <p>Add Faculties</p>
  </a>
  <a class="quick-button span2" href="#">
    <i class="icon-group"></i>
    <p>Add Faculty Advisors</p>
  </a>
</div>


<div class="row-fluid sortable " id="add" style="display: none">
  <div class="box span12">
    <div class="box-header" data-original-title>

      <h2><i class="halflings-icon white edit"></i><span class="break"></span>Add new User</h2>
      <div class="box-icon">
        <a href="#" class="btn-minimize"><i class="halflings-icon white chevron-up"></i></a>
        <a href="#" class="btn-close"><i class="halflings-icon white remove"></i></a>
      </div>
    </div>
    <div class="box-content">
      <form enctype='multipart/form-data' method="post" class="form-horizontal" action="">
        <div class="control-group">
          <label class="control-label" for="fileInput">Select file</label>
          <div class="controls">
            <input class="input-file uniform_on" id="file" name="file" type="file">
            <input type="submit" class="blue btn" value="Add">

          </div>
          <br>
          <div class="controls">
            <a href="">Or add single user</a>
          </div>
        </div>

      </form>
    </div>
  </div>
</div>