在html中打印Jquery字符串值

时间:2013-09-25 04:05:08

标签: jquery html

在以下代码中,有任何可能的方法在td

中打印字符串名称

        

<script>

var name = "Myname"

</script>


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

6 个答案:

答案 0 :(得分: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>

答案 1 :(得分:3)

像对象或td

一样为目标id添加标识符
<td class="name">i have to print the name inside here</td>

然后

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

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

答案 2 :(得分: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>

答案 3 :(得分:3)

如果你不能改变任何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>

答案 4 :(得分: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);
                }  
            );

答案 5 :(得分: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>

的jsfiddle: