我有一个jquery代码,当用户点击菜单按钮(例如:捐赠按钮)时,它会更改页面的文章部分(不直接作为子项与菜单部分相关)使用javascript拉取新信息。 DOM没有显示这些新信息,因此当我尝试应用CSS类时,它不起作用。
如何点击捐赠按钮,以便在文章部分创建新信息,但它也将CSS类应用于它。
以下是提供想法的代码的一部分:
$(document).on('click', '.menu', function () {
var option = $(this).text().replace(/á/g, 'a').replace(/é/g, 'e').replace(/í/g, 'i').replace(/ó/g, 'o').replace(/ú/g, 'u').toLowerCase();
$('.active').removeClass("active");
$(this).addClass("active");
$('#context').html(menu[option]).find('form').addClass("centerB");
change = true;
});
<div class="secContainer"></div>
<nav>
<ul>
<li><a href="index.html" id="index" class="menu">Home</a></li>
<li><a href="#" class="menu">Population</a></li>
<li><a href="#" class="menu">Life Style</a></li>
<li><a href="#" class="menu">Work</a></li>
<li><a href="#" class="menu">Comments</a></li>
<li><a href="#" class="menu">Gallery</a></li>
<li class="last"><a href="#" class="menu">Donations</a></li>
</ul>
.....
<article id="context">
<h2>HISTORY</h2>
<p>Indians once lived with machine guns and aliens. They formed highly trained tech industries and ate bananas. They trained playing mario bros all day long until reaching the boss in less than 5 minutes.</p>
</article>
基本上,他们点击主菜单上的捐款按钮。这会将article元素的内容更改为其他内容。这个东西不能直接在网页上加载,而是从具有文本的javascript对象中提取。
这一行负责改变上下文:
$('#context').html(menu[option]).find('form').addClass("centerB");
我添加了“.find('form')。addClass(”centerB“);”看它是否有效。它没。我想将CSS应用于最近提取的信息。如何将CSS或其他更改应用于此类不存在的dom信息。如果我的术语或知识错误,并且你认为更好的标题可能是合适的,请纠正我。