jQuery打印变量的值 [英] jQuery print value of variable

查看:740
本文介绍了jQuery打印变量的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是jQuery的新手,我需要一些非常简单的代码来提供帮助.因此,我有一个字符串变量,要使用listprice类在p中打印.

I am new to jQuery and I need some assistance with very simple code. So, I have a string variable that I want to print in the p with listprice class.

<script> 

$(document).ready( function() {
var string = "US $257.31";
$('.listprice).append(string);
}
</script>

有什么想法可以实现吗?

Any ideas how can I achieve that?

谢谢

H

推荐答案

您可以这样做

var string = "US $257.31";
$('.listprice').html(string);

http://jsfiddle.net/jasongennaro/akpn3/

当然,这是假定p中没有任何内容,因为它将替换其中的所有内容.

Of course, this assumes there is nothing in the p as it would replace everything in there.

要在p包含内容时将其添加到单独的行上,请执行以下操作

To add it on a separate line when the p contains content, do this

$('.listprice').append('<br />' + string);

http://jsfiddle.net/jasongennaro/akpn3/1/

编辑

根据您的评论

您知道我该如何使用普通的javascript吗?我觉得他们 正在阻止jquery.

Do you know how can I do that with regular javascript? I think they are blocking jquery in there.

您可以这样做

var string = "US $257.31";

var a = document.getElementsByTagName('p');

a[0].innerHTML += string;

http://jsfiddle.net/jasongennaro/akpn3/2/

这将找到第一个p并添加变量string.

This finds the first p and adds the variable string.

这篇关于jQuery打印变量的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆