使用 jQuery 替换 H3 标题中的文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12898475/
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 12:06:37  来源:igfitidea点击:

Using jQuery to replace text inside H3 header

jquerystringdomtextreplace

提问by user1729506

I tried to keep the code as simple/clean as possible. There is an h3 inside of two DIVs. The top DIV has an ID, the inner DIV a unique class.

我试图保持代码尽可能简单/干净。两个 DIV 里面有一个 h3。顶部 DIV 有一个 ID,内部 DIV 有一个唯一的类。

$("#Events .event-header h3").html("your new header");

I also tried:

我也试过:

$("#Events .event-header h3").text("your new header");

Neither work. When I try them in the Chrome console, it tells me that the element I'm referencing is undefined and it doesn't know what text to target. The DIV structure couldn't be more simple and clear. I don't know how else to reference the h3 tag.

都不工作。当我在 Chrome 控制台中尝试它们时,它告诉我我引用的元素未定义,它不知道要定位什么文本。DIV 结构再简单不过了。我不知道如何引用 h3 标签。

Any tips? (I've fixed the typo in the .event-header class - Apparently, posting comments instead of doing that previously is a grave sin. Happy Halloween?)

有小费吗?(我已经修复了 .event-header 类中的错字 - 显然,发表评论而不是以前这样做是一种严重的罪过。万圣节快乐?)

回答by VVV

Your class is .event-headernot .events-header

你的课.event-header不是.events-header

There is an extra "s" at events.

事件中有一个额外的“s”。

So try this

所以试试这个

$("#Events .event-header h3").text("your new header");

Fiddle

小提琴

回答by loQ

Maybe you have conflict in your sites jquery using $ sign? Why dont you try wrapping it with this ready statement instead

也许您在使用 $ 符号的站点 jquery 中存在冲突?你为什么不尝试用这个 ready 语句来包装它呢?

jQuery(document).ready(function($){
  $("#Events .events-header h3").text("your new header");?
});