标签内的其他属性将值传递给函数 [英] Additional attributes within a tag to pass values to a function

查看:109
本文介绍了标签内的其他属性将值传递给函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 <td><img 
    name="Brown Corkboard"
    title="800.00"
    id="nikolas"
    src="materials/corkboardtexture.png"
    alt="600"
    onclick="reply_click(this.src,this.name,this.title,this.alt)"
    width="200"
    height="150"     

。我使用attribues title和alt来指定执行一个函数时需要的一些值,这个函数在点击图像时被调用。

I have this image in an HTML table. I am using the attribues title and alt to specify some values that are needed in performing a function that is called upon clicking the image.

我必须走错路。点击图像后,我想要将多个不同的变量发送到我的功能。然而,我已经超出了选项,因为我已经用完了title属性和alt属性。任何建议?

I must be going the wrong way. I want a number of different variables sent to my function upon clicking the image. However i am out of options since i have already used up the title attribute and the alt attribute. Any suggestions?

推荐答案

正如其他人所回答的, data - 属性是如果你需要在HTML属性中携带任意的数据,那么 的路要走。 alt title 属性不应该用于这样的目的,因为这会干扰定义的含义和使用这些属性。

As others have replied, data- attributes are the way to go if you need to carry arbitrary data in HTML attributes. The alt and title attributes should not be used for such purposes at all, since that would interfere with the defined meanings and use of those attributes.

这个例子并没有说明你为什么不能简单地在函数调用中写入数据作为参数,比如 onclick = reply_click(this.src,this.name,800.00,600)

The example does not show any reason why you cannot simply write the data as arguments in the function invocation, like onclick="reply_click(this.src, this.name, 800.00, 600).

或者你可以在JavaScript对象中写入数据,在DOM之外,并使用你的函数来获取它们,例如使用 id 属性值作为键。

Or you could write the data in JavaScript object, within or outside the DOM, and make your function just pick them up, using e.g. the id attribute value as the key.

这篇关于标签内的其他属性将值传递给函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆