尝试从目标事件中获取数据时在IE8中遇到问题 [英] Having problems in IE8 when attempting to get data from target event

查看:172
本文介绍了尝试从目标事件中获取数据时在IE8中遇到问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的代码..

// Get some data
var id = event.target.id;
var flag_status = event.target.dataset.change;
var winner_id = event.target.dataset.winner;
var item_id = event.target.dataset.item;

普通浏览器,例如 Firefox Chrome 获取价值没有任何问题,一切都很好;但是 IE8 没有发生任何事情,所以我假设它无法获取数据。

"Normal" browsers like Firefox and Chrome get the values with no problems and everything works great; however nothing is happening with IE8 so I'm assuming it can't get the data.

event 参数通过以下代码传递给此函数:

The event parameter is passed to this function via this code:

$('.shipping_status').click(function(event) {

    event.preventDefault();

    // Update Shipping Status
    updateShippingStatus(event);

});

..然后在点击其中一个示例元素时获取它:

..and then in turn gets it when one of these example elements is clicked:

<a title="Item Being Processed" class="shipping_status processing" data-item="102383" data-winner="172" data-change="0" id="processing_102383" href="#"></a>                            
<a title="Item Posted" class="shipping_status posted active" data-item="102383" data-winner="172" data-change="1" id="posted_102383" href="#"></a>
<a title="Problem With Item" class="shipping_status problem" data-item="102383" data-winner="172" data-change="3" id="problem_102383" href="#"></a>
<a title="Item Delayed" class="shipping_status delayed last" data-item="102383" data-winner="172" data-change="2" id="delayed_102383" href="#"></a>

我有没有办法让它与IE8一起使用? ...另外,我没有 IE9 + 来测试 - 有没有人知道它是否适用于> IE8?

Is there a way I can get this to work with IE8? ...additionally, I don't have IE9+ to test with - does anyone know if it works in > IE8?

我还用 jQuery 标记了这一点,如果我们有另外的方法来获取这个数据,那么jQuery也能用于IE8。

I have also tagged this with jQuery if there us alternative way to get this data with jQuery that will work with IE8 as well.

推荐答案

看起来IE刚开始支持IE11中的数据集属性: https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement.dataset

Looks like IE just started supporting the dataset attribute in IE11: https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement.dataset

尝试使用 getAttribute()

var flag_status = event.target.getAttribute('data-change');
var winner_id = event.target.getAttribute('data-winner');
var item_id = event.target.getAttribute('data-item');

如果您使用的是jQuery,那么使用数据()可以轻松实现方法:

If you are using jQuery, it makes this easy using the data() method:

var $target = $(event.target);
var flag_status = $target.data('change');
var winner_id = $target.data('winner');
var item_id = $target.data('item');

如果属性只使用 data()在HTML元素上不会改变。来自 jquery文档

Only use data() if the attributes on the HTML element won't change. From jquery documentation:


数据属性是在第一次访问数据属性为
时被提取的,然后不再被访问或变异(所有数据值
随后都存储在jQuery内部。)

The data- attributes are pulled in the first time the data property is accessed and then are no longer accessed or mutated (all data values are then stored internally in jQuery).

如果您需要重新读取更改的HTML属性,只需使用 attr()方法:

If you need to re-read HTML attributes that change, just use the attr() method:

var $target = $(event.target);
var flag_status = $target.attr('data-change');
var winner_id = $target.attr('data-winner');
var item_id = $target.attr('data-item');

这篇关于尝试从目标事件中获取数据时在IE8中遇到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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