在<li> <a> in javascript

时间:2017-01-08 08:23:05

标签: javascript

I am trying to get the text within

<li><a href="">Navi</a><li> 

back using Javascript.

var clickedTopPlayer = function()
{

	var player = this.innerHTML;

	var parsed = player.innerHTML();

	loadResults(parsed);

    return false;
};
$('li').on("click", clickedTopPlayer);

The code I tried to do this with wouldn't work. Any ideas?

1 个答案:

答案 0 :(得分:0)

只需使用innerText属性:

var clickedTopPlayer = function() {
    var player = this.innerText;
    loadResults(player);
    return false;
};

$('li').on("click", clickedTopPlayer);

见这里:

&#13;
&#13;
var clickedTopPlayer = function() {
  var player = this.innerText;
  console.log(player);
};

$('li').on("click", clickedTopPlayer);
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<li><a>Foo</a>
</li>
&#13;
&#13;
&#13;

另请注意,innerHTML永远不是一个函数 - 正如您在此处使用它一样

var parsed = player.innerHTML();