問題描述
有沒有辦法檢查用戶代理是否支持拖動(dòng)&刪除特定元素(div、tr、...)?
Is there a way to check if a user agent supports drag & drop of a particular element (div, tr,...)?
我有一個(gè)帶有可拖動(dòng)表格行的表格,它在 Chrome 中運(yùn)行良好.但是由于 Internet Explorer(最高版本 9)只支持圖像和鏈接(以及其他一些)的拖動(dòng),我想在表格行內(nèi)給它一個(gè)小的錨點(diǎn)拖動(dòng)句柄.我的代碼如下所示:
I've got a table with draggable table rows, which works fine in Chrome. But since Internet Explorer (up to version 9) only supports dragging of images and links (and a few others), I wanted to give it a little anchor drag handle inside the table row instead. My code looks something like this:
<table>
<tr draggable="true">
<td><a class="drag-handle"></a> Hello World</td>
</tr>
</table>
JS/jQuery:
$('tr[draggable]').each(function() {
// Remove draggable="true" from <tr>
$(this).attr('draggable', '');
// Add draggable="true" to <a class="drag-handle">
$(this).find('.drag-handle').attr('draggable', 'true');
});
顯然,要真正完成這項(xiàng)工作還有很多工作要做,但要點(diǎn)是:如果用戶的客戶端不支持拖動(dòng) a (IE<10),則給用戶一個(gè)拖動(dòng)的東西.
Obviously, there is a lot more to do to actually make this work, but you get the gist: giving the user a drag thingy if their client doesn't support dragging of a (IE<10).
現(xiàn)在我只想在用戶的客戶端不支持拖動(dòng)表格行的情況下進(jìn)行替換.是否有任何已知的特征檢測?
Now i want to do the replacement only if the user's client doesn't support dragging of table rows. Is there any known feature detection for that?
推薦答案
來自 檢測 javascript 中的 HTML5 拖放支持:
if('draggable' in document.createElement('span')) {
alert("Drag support detected");
}
感謝 Mark Rhodes 的提示!還要感謝 micha 指出我的 jQuery 錯(cuò)誤 (removeAttr('draggable')
而不是attr('draggable')
).
Thanks to Mark Rhodes for the tip! Also thanks to micha for pointing out my jQuery error (removeAttr('draggable')
instead of attr('draggable')
).
這篇關(guān)于如何檢查 IE Drag &放棄對(duì)任何元素的支持的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!