在jQuery中设置文本 [英] set text in jquery

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

问题描述

如果我要将<div id="error"></div>的文本设置为在此处测试消息",我该怎么做:

If I want to set the text of a <div id="error"></div> to "Test message here", do I do:

 $('<div id="error">').text('Test message here');

我尝试了此操作,但无法正常工作.有想法吗?

I tried this and it's not working. Thoughts?

推荐答案

您创建了一个新的div并设置了它的文本,但是您没有在任何地方插入它.您需要做的是:

You create a new div and set its text, but you don't insert it anywhere. What you need to do is:

var el = $('<div id="error">').text('Test message here');
$(document).append(el);

或者,如果div已经存在:

or, if the div is already there:

$("#error").text('Test message here');

这篇关于在jQuery中设置文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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