简体   繁体   English

如何将新创建的html元素绑定到其CSS

[英]how to bind newly created html element to its css

i am adding dynamically new html elements thru javascript, and those newly created html elements have a css class which already exists in a linked style.css file. 我正在通过javascript动态添加新的html元素,并且那些新创建的html元素具有一个CSS类,该类已经存在于链接的style.css文件中。 how do i let this newly created element know about his css class, so that the styling by this class can effect the newly created html element. 我如何让这个新创建的元素知道他的CSS类,以便此类的样式可以影响新创建的html元素。

my js: 我的js:

for(k=1;k<=5;k++){
  if(k==data[i]['price']){
html = html + '<input class=\"star\" type=\"radio\" name=\"test-5\" disabled=\"disabled\" value=\"'+k+'\" checked=\"checked\" />';
  }else{
html = html + '<input class=\"star\" type=\"radio\" name=\"test-6\" disabled=\"disabled\" value=\"'+k+'\"/>';
  }
 }

that class star should be popular to css file right after its creation. 该类star创建后应立即在css文件中流行。 is there any make-popular function in jquery or so? jQuery中是否有任何make-popular功能?

hope that i explained my problem well.. 希望我能很好地解释我的问题。

thanks 谢谢

As long as the page already references the CSS file, then all you have to do is create the element with the class="star" attribute. 只要页面已引用CSS文件,那么您要做的就是创建具有class =“ star”属性的元素。 I don't see what you ultimately do with the html variable, but assuming your element appears on the page with the class="star" attribute and you have a star css class declared properly, then that is all that is needed. 我看不到最终要使用html变量做什么,但是假设您的元素出现在具有class="star"属性的页面上,并且您有正确声明的star css类,则仅此而已。 Use an F12 developer tool to inspect the element in the browser to verify the class is being applied. 使用F12开发人员工具检查浏览器中的元素,以验证是否正在应用该类。

Example: 例:

http://jsfiddle.net/mQKdc/ http://jsfiddle.net/mQKdc/

$(function(){
     $('#addToMe').html('<div class="star">test</div>');
});

Edit: Note however, that input elements are difficult to style, because each OS(Win/Linux) and/or browser(IE,Firefox,Chrome) can provide their own styling of input elements. 编辑:但是请注意,输入元素很难设置样式,因为每个OS(Win / Linux)和/或浏览器(IE,Firefox,Chrome)都可以提供自己的输入元素样式。 There are certain things you may find are difficult to style on them. 您可能会发现某些难以在其上样式化的东西。 In which case it's not really a javesceiprt problem, but a CSS problem. 在这种情况下,这实际上不是javesceiprt问题,而是CSS问题。 You'd need to ask a CSS related question showing exactly what 1) your CSS is, 2) what your goal is, 3) what your are seeing that indicates your current CSS doesn't meet the goal(Ie your problem). 您需要问一个与CSS相关的问题,确切地说明1)您的CSS是什么,2)您的目标是什么,3)您所看到的表明您当前的CSS达不到目标(即您的问题)。

Edit 2: Before sorting, the element's HTML: 编辑2:在排序之前,元素的HTML:

<div role="text" aria-label="" class="star-rating rater-0 star star-rating-applied star-rating-readonly star-rating-on">
   <a title="2">2</a>
</div>

The input is there too, but the style is being applied to this a element. 输入也在那里,但是样式已应用于此元素。 The star styling is applied to <a> elements: 星型样式应用于<a>元素:

div.star-rating a {
background: url(../images/star.gif) no-repeat 0 0px;
}

After sorting, the div+a doesn't exist, only the input. 排序后,div + a不存在,只有输入。 So the elements that the style applies to( div.star-rating a do not exist : 这样的风格适用于(元素div.star-rating a不存在

<input class="star" type="radio" name="test-1-rating-5" disabled="disabled" value="4">

So the HTML you are generating after sorting is incorrect, based on the HTML I'm seeing before sorting. 因此,基于我在排序之前看到的HTML,排序后生成的HTML是不正确的。 Are you using some sort of plugin/library to generate the stars? 您是否正在使用某种插件/库来生成星星? If so, my guess is there is some function you would call to generate the proper HTML. 如果是这样,我想您会调用某些函数来生成正确的HTML。

Solution: Looks like you are supposed to do something like this to run the .rating() function on each element you want the Star Rating Plugin to be activated on: 解决方案:您似乎应该执行以下操作,才能在要激活星级评定插件的每个元素上运行.rating()函数:

$('.star').rating();

You just need to run this after generating the new elements. 您只需要在生成新元素之后运行它即可。

If you are adding new html elements which are using existing Css, then those should be able to to use them by default. 如果要添加使用现有Css的新html元素,则默认情况下这些元素应能够使用它们。 Are you having any issue with that? 你有什么问题吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM