【发布时间】:2014-10-08 17:47:56
【问题描述】:
我有以下代码:
for(var i = 0;i < choices.length; i++){
var $plans = $("<li id='entirePlan' class='Solution_ID'></li>").html('<p id="plan_id" class="plan_class">Plan: '+(i+1)+'</p>');
}
这会在我的网站上创建最多三个不同的计划,并相应地标记。 (可以有 1 个计划、2 个计划或 3 个计划)。如果这三个都存在,它看起来像
计划:1
-----计划信息在这里----
计划:2
-----计划信息在这里----
计划:3
-----计划信息在这里----
我希望能够检查每个计划中的信息,如果符合某些条件,则隐藏该计划。像这样:
if(myCondition === something){
if(plan1 exists){
//hide plan 1
}
}else if(myCondition === something){
if(plan2 exists){
//hide plan 2
}
}else if(myCondition === something){
if(plan3 exists){
//hide plan 3
}
}
但是,为了隐藏它,我无法获得计划的编号。这是动态生成的计划之间的唯一区别。我尝试了以下方法:
$('p#plan_id.plan_class'))
$('#plan_id'))
$('.plan_class'))
$('#plan_id.plan_class'))
然后是每个末尾带有 .html() 或 .text() 的内容。我不知道它是什么计划编号。我想过尝试做一些类似的事情:first,:second,:third,但也无法以这种方式获得价值。
我正在使用 .js 文件,但更喜欢先使用 jquery。 javascript 作为最后的手段。建议?
--更新: 我也尝试了 Luizgrs 的答案。我尝试将数据编号添加到我的
带有我的标题和解决方案 ID 旁边。我在 console.log 中进行了测试,看看我是否得到了我需要的东西。我用过:
console.log("plan?", $('.Solution_ID').data('number'));
console.log("plan?", $('.Solution_ID[data-number=1]'));
console.log("plan?", $('.plan_class').data('number'));
console.log("plan?", $('.plan_class[data-number=1]'));
我得到了:
我在该对象中远程看到的唯一需要的东西是在 childElementcount = 1 下。我尝试将 childElementcount 附加到上述调用的末尾,但出现错误。
我在下面使用 console.log 尝试了 typeoneerror 的建议解决方案,看看我是否能得到我想要的:
console.log("plan?", $('#entirePlan_1 p'));
console.log("plan?", $('#entirePlan_1 p').text());
console.log("plan?", $('#entirePlan_1 p').html());
结果: 不工作的家伙,我不知道为什么..
【问题讨论】:
-
您是否有在线或 jsbin 上的工作示例,以便我们查看?从这个问题有点难以理解。
-
你不能在一页上使用几个id
-
所以你想根据它的索引号来获取计划,对吧?猜测像
:first :second :third这样的语法并不是解决问题的有用方法。 DOM selection 使用 jQuery 有充足的文档。
标签: javascript jquery html css title