![](https://iknow-base.cdn.bcebos.com/lxb/notice.png)
javascript里面的index属性问题
<body><divid="box"><strong><spanstyle="color:red">*</span>总体评价</strong><imgsrc="img/1...
<body>
<div id="box">
<strong><span style="color:red">*</span>总体评价</strong>
<img src="img/1.jpg" alt="" class="star1" style="index:1" />
<img src="img/1.jpg" alt="" class="star2" style="index:2" />
<img src="img/1.jpg" alt="" class="star3" style="index:3" />
<img src="img/1.jpg" alt="" class="star4" style="index:4" />
<img src="img/1.jpg" alt="" class="star5" style="index:5" />
<img src="img/4.jpg" id="notice" />
</div>
</body>
我这样加了index属性,但是只有IE下面是对的。但是其他浏览器都是undefined。 而且好像this.style.index也不好用,IE下也不行。帮忙看看是为什么 展开
<div id="box">
<strong><span style="color:red">*</span>总体评价</strong>
<img src="img/1.jpg" alt="" class="star1" style="index:1" />
<img src="img/1.jpg" alt="" class="star2" style="index:2" />
<img src="img/1.jpg" alt="" class="star3" style="index:3" />
<img src="img/1.jpg" alt="" class="star4" style="index:4" />
<img src="img/1.jpg" alt="" class="star5" style="index:5" />
<img src="img/4.jpg" id="notice" />
</div>
</body>
我这样加了index属性,但是只有IE下面是对的。但是其他浏览器都是undefined。 而且好像this.style.index也不好用,IE下也不行。帮忙看看是为什么 展开
43个回答
展开全部
CSS 里没有 index 这个属性,类似 style="index: 1" 是无效的,如果你是想自定义一个 index 属性的话,不应该把它写到 style 里,而是使用 dataset,示例:
<img src="img/1.jpg" alt="" class="star1" data-index="1" />
获取时:
document.querySelector('img.star1').dataset.index; // 返回 1
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
如果就想要这个index,没有必要这么做
<body>
<div id="box">
<strong><span style="color:red">*</span>总体评价</strong>
<img src="img/1.jpg" alt="" class="star1" index="1" />
<img src="img/1.jpg" alt="" class="star2" index="2" />
<img src="img/1.jpg" alt="" class="star3" index="3" />
<img src="img/1.jpg" alt="" class="star4" index="4" />
<img src="img/1.jpg" alt="" class="star5" index="5" />
<img src="img/4.jpg" id="notice" />
</div>
</body>
var index = this.getAttribute('index');
//前提是这个this是指向 某一个 img 标签
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
index是什么鸟属性? 标准css中是没有这个属性的。 只有表示空间层次的 z-index
追问
不是可以把一个变量加入到 style属性里面? index是自定义的吧应该
追答
style里面不能用变量!!! 谁告诉你style中可以用变量的。。。。
要使用变量除非是以下情况下才可以:
1. 该 html是嵌入在编译或解释型语言中,如php或者jsp 或者其他的html模板语言 如ejs,jade,handbarjs等。
变量的写法可以就根据不同的模板引擎来,如: 或或{{index}}等
这样这些html引擎就会吧变量渲染成相应的值。也就是说该html必须经过后台编译或者前台js库进行编译才能使用变量写法。html和css 标准是不可能支持变量的。如果是微软ie这个奇葩浏览器就当我没说。
2. 引用css文件。
在css文件中,可以通过定义变量等方式 来进行 样式的维护。当然这是需要 less 或者其他的css解释器进行翻译才可以使用。
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
不需要在style里加 style里加的是样式
直接和class、style、src属性一样的形式写自定义属性
<img id='myimg' src="img/1.jpg" alt="" class="star1" index=1 />
这样 $('#myimg').attr('index');就是属性值
没学jquery就用原生js获取index属性 和获取src属性值一样
直接和class、style、src属性一样的形式写自定义属性
<img id='myimg' src="img/1.jpg" alt="" class="star1" index=1 />
这样 $('#myimg').attr('index');就是属性值
没学jquery就用原生js获取index属性 和获取src属性值一样
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
如果你想要获取你下面的img元素索引 也就是他的位数 你可以使用js获取并改变元素
<<html>
<head>
<script type="text/javascript">
function alertIndex()
{
var x=document.getElementById("mySelect").selectedIndex;
var y=document.getElementsByTagName("option");
alert(y[x].text + " has the index of: " + y[x].index);
}
</script>
</head>
<body>
<form>
Select your favorite fruit:
<select id="mySelect">
<option>Apple</option>
<option>Orange</option>
<option>Pineapple</option>
<option>Banana</option>
</select>
<br />
<br />
<input type="button" onclick="alertIndex()"
value="Show index of the chosen fruit">
</form>
</body>
</html>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询