[英]How retrieve innerHTML of an anchor tag using jQuery?
我有一個這樣的Bootstrap表,
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<table class="table table-striped table-bordered">
<tr><th>First Name</th><th>Last Name</th><th>Age</th></tr>
<tr><td><a class="cl" href="" onclick="call()">Mickey</a></td><td>Mouse</td><td>5</td>
<tr><td><a class="cl" href="" onclick="call()">Tom</a></td><td>Cat</td><td>6</td>
<tr><td><a class="cl" href="" onclick="call()">Pooh</a></td><td>Bear</td><td>4</td>
<tr><td><a class="cl" href="" onclick="call()">Donald</a></td><td>Duck</td><td>7</td>
<tr><td><a class="cl" href="" onclick="call()">Jerry</a></td><td>Mouse</td><td>8</td>
</table
<br /><br />
<div id="output"></div>
<script>
$('.cl').click(function() {
var t = $(this).text();
$('#output').html(t);
});
</script>
</body>
</html>
名字列由超鏈接組成。 當用戶單擊任何一個名字時,該值應返回到輸出<div>
而不進行任何頁面重定向。
我在控制台中收到此錯誤,
"ReferenceError: call is not defined
at HTMLAnchorElement.onclick (http://null.jsbin.com/runner:1:993)"
我在這里做錯了什么?
這是我到目前為止所做的工作的鏈接-http://jsbin.com/loqikixesa/edit?html,控制台,輸出
我相信你想要這個(使用不是val的文字):
var t = $(this).text();
只需阻止anchor
標記的默認操作,並使用.text()
而不是.val()
,
$('.cl').click(function(e) {
e.preventDefault();
$('#output').html($(this).text());
});
還要刪除這些元素的內聯處理程序。 那會造成沖突。
你只做這個
$('.cl').click(function() {
var t = $(this).text();
$('#output').append(t);
});
如果現在可以使用val或text,請檢查val()和text()之間的區別
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.