javascript - 您如何唯一地定位函数实例的 html/css?
问题描述
您可以在不将 id 作为参数传递的情况下唯一地定位对象的新实例吗?
如果您从构造函数创建新对象并且每个实例都可以访问存储为构造函数中的变量的一些 html(表示),当创建对象的新实例并且从表示变量输出 html 时,您如何定位每个这些实例以独特的方式?您如何设计一个实例与另一个实例不同的样式?
function MyObj() {
this.content = "<div class = 'someclass'>Some text </div>";
}
var newObj1 = new MyObj();
console.log(newObj1.content); //outputs Some text
var newObj2 = new MyObj();
console.log(newObj2.content); //outputs Some text
如果您想针对所有新类的 css,您可以使用:
var target1 = $('.someclass').css('color,'orange');
但是如何以不同的方式定位一个目标呢?
解决方案
这取决于您希望如何定位另一个实例。一种选择是使用:nth-child
、:first-child
或:last-child
选择器。举个例子:
$('.someclass:nth-child(even)').css('color','orange');
$('.someclass:nth-child(odd)').css('color','blue');
$('.someclass:nth-child(1)').css('color','green');
$('.someclass:first-child').css('color','red');
推荐阅读
- android - 自动 Java 到 Kotlin 的转换:未解决的对“ARG_LAYOUT_RES_ID”的引用
- amazon-web-services - 如何知道哪个 s3 存储桶触发了哪个 lambda?
- vba - 在其他子函数中调用对象的事件函数集
- c# - 在运行时无法从程序集中获取方法
- sql-server - 对所有数据库执行查询
- php - 登录奏鸣曲系统用户
- oracle - Maven soa 使用 soa 插件进行测试
- asp.net - 如何在 Web 应用程序中创建新的 ASP.NET Web 表单的超链接
- python - python - 如何使用正则表达式来分隔输入
- android - android不能添加一个信号