开发者

Jquery Replace content in runtime issue

开发者 https://www.devze.com 2023-04-09 12:35 出处:网络
What I am trying to accomplish is, using jQuery, dynamically replace the content (generated from another javascript) of div tag with id=\"Test\".

What I am trying to accomplish is, using jQuery, dynamically replace the content (generated from another javascript) of div tag with id="Test".

<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.pack.js"></script>
<script type="text/javascript">
$(function(){
    $("#Test").html('<b>test content</b>');
});
</script>
</head>
<body id="testAgain">
begin
<div class="scrollable">   
    <div class="items" id="Test">
    </div>
</div>
end
</body>
</html>

As you can see,

$("#Test").html('<b>test content</b>');

replaces the html code of div tag with id="Test" with html code

<b>test content</b>

this works perfectly fine. I am getting rendered ht开发者_如何学编程ml as

begin 
test content
end 

<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.pack.js"></script>
<script type="text/javascript">
function TestScript(){
document.write('<b>test content</b>');
    };
</script>
<script type="text/javascript">
$(function(){
    $("#Test").html(TestScript());
});
</script>

</head>
<body id="testAgain">
begin
<div class="scrollable">   
    <div class="items" id="Test">
    </div>
</div>

end
</body>
</html>

Here,

$("#Test").html(TestScript());

replaces the html code of div tag with id="Test" with generated html from javascript

TestScript()

I am getting rendered html as

test content

It replaces the entire content of the html file. How to resolve this problem?

Kindly help.

Thanks, John


Try this:

<script type="text/javascript">
function TestScript(){
    return '<b>test content</b>';
};
</script>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号