如何用不同的ID调用相同的方法

时间:2022-02-13 23:55:37

I have html controls of two sets with different ids in my page, when I click a tag I want to call DoSomething method.

我在页面中有两个具有不同ID的html控件,当我单击一个标签时我想调用DoSomething方法。

<div>
   <a href="javascript:void(0);" onclick="DoSomething();">
   <span id="spnValue" >Value1</span>
</div>

<div>
   <a href="javascript:void(0);" onclick="DoSomething();">
   <span id="spnValue1">Value2</span>
</div>

function DoSomething() {
   var htmlVal = "";
   skuList  = $("span[id*='spnValue']").html();
}

But whichever one I click it gives the Value1 in htmlVal. How can I distinguish and retrieve the value of method called

但无论我点击哪一个都给出了htmlVal中的Value1。如何区分和检索所调用方法的值

3 个解决方案

#1


5  

You can pass clicked element object to method:

您可以将单击的元素对象传递给方法:

<a href="javascript:void(0);" onclick="DoSomething(this);">
<span id="spnValue1">Value2</span>
</div>

and then use it for traversing to child span element:

然后使用它来遍历子span元素:

function DoSomething(obj) {
  var htmlVal = "";
  skuList  = $(obj).find('span').html();
}

Working Demo

#2


1  

<div>
       <a href="javascript:void(0);" onclick="DoSomething(this);">
      <span id="spnValue1">Value2</span>
</div>

function DoSomething(obj) {
    var htmlVal = "";
    skuList  = $(obj).parent().find('span').html();
}

#3


0  

You can pass the current element in the function using that you can locate the span element which is a children on the a tag

您可以使用您可以在a标记上找到span元素的span元素来传递函数中的当前元素

<div>
  <a href="javascript:void(0);" onclick="DoSomething(this);">
     <span id="spnValue" >Value1</span>
  </a>
</div>

<div>
  <a href="javascript:void(0);" onclick="DoSomething(this);">
     <span id="spnValue1">Value2</span>
  </a>
</div>

function DoSomething(node) {
   var htmlVal = "";
   skuList  = $(node).find('span').html();//for children
   //skuList  = $(node).next('span').html();//for siblings
}

#1


5  

You can pass clicked element object to method:

您可以将单击的元素对象传递给方法:

<a href="javascript:void(0);" onclick="DoSomething(this);">
<span id="spnValue1">Value2</span>
</div>

and then use it for traversing to child span element:

然后使用它来遍历子span元素:

function DoSomething(obj) {
  var htmlVal = "";
  skuList  = $(obj).find('span').html();
}

Working Demo

#2


1  

<div>
       <a href="javascript:void(0);" onclick="DoSomething(this);">
      <span id="spnValue1">Value2</span>
</div>

function DoSomething(obj) {
    var htmlVal = "";
    skuList  = $(obj).parent().find('span').html();
}

#3


0  

You can pass the current element in the function using that you can locate the span element which is a children on the a tag

您可以使用您可以在a标记上找到span元素的span元素来传递函数中的当前元素

<div>
  <a href="javascript:void(0);" onclick="DoSomething(this);">
     <span id="spnValue" >Value1</span>
  </a>
</div>

<div>
  <a href="javascript:void(0);" onclick="DoSomething(this);">
     <span id="spnValue1">Value2</span>
  </a>
</div>

function DoSomething(node) {
   var htmlVal = "";
   skuList  = $(node).find('span').html();//for children
   //skuList  = $(node).next('span').html();//for siblings
}