jQuery 打印变量的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6916300/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 21:33:24  来源:igfitidea点击:

jQuery print value of variable

jquery

提问by bobek

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.

我是 jQuery 的新手,我需要一些非常简单的代码的帮助。所以,我有一个字符串变量,我想用 listprice 类在 p 中打印它。

<script> 

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

Any ideas how can I achieve that?

任何想法我怎么能做到这一点?

Thank you,

谢谢,

H

H

回答by Jason Gennaro

You could do this

你可以这样做

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

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

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

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

当然,这假设里面没有任何东西,p因为它会替换那里的所有东西。

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

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

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

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

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

EDIT

编辑

As per your comment

根据你的评论

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

你知道我怎么能用普通的 javascript 做到这一点吗?我认为他们在那里阻止了 jquery。

You could do this

你可以这样做

var string = "US 7.31";

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

a[0].innerHTML += string;

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

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

This finds the first pand adds the variable string.

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

回答by James Montagne

You're missing a '.

你缺少一个'.

$('.listprice).append(string);

should be

应该

$('.listprice').append(string);

EDIT: Also missing a )at the end.

编辑:最后也缺少一个)

Other than that it's fine.

除此之外还好。

回答by Mrchief

You've it right except for some typos (and I added tag specifier to make the lookup bit faster):

除了一些拼写错误外,您说对了(我添加了标签说明符以使查找速度更快):

$(document).ready( function() {
   var string = "US 7.31";
   $('p.listprice').append(string);
}

回答by Christian Mann

The above code will append US $275.31to all elements with the listpricetag.

上面的代码将附加US $275.31到所有带有listprice标签的元素。

If you want to append it only to ptags with listprice, specify $('p.listprice').append(....

如果您只想将其附加到p带有 的标签listprice,请指定$('p.listprice').append(...

Oh, and remember to close your quotes after the identifier (listprice).

哦,请记住在标识符 ( listprice)后关闭引号。

回答by Erick Petrucelli

You aren't appending (adding an element to another). You just want to change the ptext.

您没有追加(将元素添加到另一个元素)。您只想更改p文本。

$(".listprice").text(string);

回答by sonu

You Can Also Print Your Variable Value Between String Look like...

您还可以在字符串之间打印变量值,如下所示...

var filename = $('#resume').val();
$("#msg_show").text("Your resume "+filename+" has uploaded successfully. If you wish to upload a different document, please click the “Submit your Resume” button and select your file again.");