1

在下面的代码中是否有任何可能的方法来打印里面的字符串名称td

<script>

var name = "Myname"

</script>


<td>i have to print the name inside here</td>
    </tr>
</table>
4

6 回答 6

4
<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>
于 2013-09-25T04:10:02.440 回答
4

如果您无法更改任何 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>
于 2013-09-25T04:20:58.853 回答
3

向目标添加标识符,td例如类或id

<td class="name">i have to print the name inside here</td>

然后

jQuery(function(){
    $('td.name').text(name)
})

注意:由于它是使用 jquery 标记的,我假设已经添加了 jQuery 库

于 2013-09-25T04:09:58.930 回答
3

试试这个脚本:

<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>
于 2013-09-25T04:14:11.260 回答
1

检查 Js 小提琴

http://jsfiddle.net/5EXtz/

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);
                }  
            );
于 2013-09-25T04:15:22.893 回答
0

也许制作自己的特殊标签....

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>

小提琴:

于 2016-05-10T10:36:27.767 回答