在下面的代码中是否有任何可能的方法来打印里面的字符串名称td
<script>
var name = "Myname"
</script>
<td>i have to print the name inside here</td>
</tr>
</table>
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script>
$(document).ready(function(){
var name = "Myname"
$("#result").html(name);
})
</script>
<div id="result">i have to print the name inside here</div>
如果您无法更改任何 html 标记,请使用以下方法:
<script>
var name = "Myname"
$(document).ready(function(){
// Replace all with `name`
$('td:contains("i have to print the name inside here")').text(name);
// Add `name` to end
$('td:contains("i have to print the name inside here")').append(name);
// Add `name` to beginning
$('td:contains("i have to print the name inside here")').prepend(name);
// etc.
});
</script>
向目标添加标识符,td
例如类或id
<td class="name">i have to print the name inside here</td>
然后
jQuery(function(){
$('td.name').text(name)
})
注意:由于它是使用 jquery 标记的,我假设已经添加了 jQuery 库
试试这个脚本:
<script>
var name = "Myname"
$("#c0r0").text(name);
</script>
对于这个生成的 html 页面:
<table>
<tr>
<td id="c0r0">I have to print the name inside here</td>
<td id="c0r1">Dummy Text</td>
<td id="c0r2">Dummy Text</td>
</tr>
..............
</table>
检查 Js 小提琴
var name = "Myname";
$('#mytable tr:first td')
.each(
function()
{
//'this' represens the cell in the first row.
var tdtxt=$(this).html();
var n = tdtxt.concat(name);
alert(n);
}
);
也许制作自己的特殊标签....
var myDataModel = {
name: 'Sam Chalupka',
favoriteFruit: 'Lemon',
age: '45'
};
$('t').each(function() {
var key = $(this).attr('data');
var text = myDataModel[key];
console.log(this);
$(this).text(text);
});
在 html...
<t data="name"></t>
<t data="favoriteFruit"></t>
<t data="age"></t>
小提琴: