如何在jquery中获取元素的值?

时间:2015-05-12 01:56:40

标签: jquery function

如何在函数data-toggle中获取load_data(id)的值?最近真的很乱。它无论我尝试什么都不起作用。

<ul class="list-unstyled" id="faq_list">

  <li  data-type='parent'>
    <a href="javascript:;"  data-toggle="a77" onClick="load_data(1)">General</a>
    <div id="target_1" class="subdata"></div>
  </li>

  <li  data-type='parent'>
    <a href="javascript:;"  data-toggle="b99" onClick="load_data(4)">Orders</a>
    <div id="target_4" class="subdata"></div>
  </li>

  <li  data-type='parent'>
    <a href="javascript:;"  data-toggle="cAB" onClick="load_data(3)">Problems</a>
    <div id="target_3" class="subdata"></div>
  </li>

</ul>

如何在函数data-toggle中获取load_data(id)标记的值?

1 个答案:

答案 0 :(得分:0)

如果你想要,你可以试试这个解决方案。

更改此内容:

<a href="javascript:;"  data-toggle="a77" onClick="load_data(1)">General</a>

进入

<a href="javascript:;"  data-toggle="a77" onClick="load_data(1, this.attributes[1])">General</a>
 // this.attributes[1] --> capture element atribute at index 1, 
 // in this case is "data-toggle" 
 // same goes to another a element

<强>的javascript

function load_data(no, dataToggle){
  console.log(dataToggle.value);    
}

===================== OR =======================

改变这一点:

 <a href="javascript:;"  data-toggle="a77" onClick="load_data(1)">General</a>

进入

<a href="javascript:;"  data-toggle="a77" onClick="load_data(1, this)">General</a>

然后在load_data函数中:

function load_data(no, elem){   
  console.log($(elem).data('toggle'));    
}

<强> DEMO