简体   繁体   English

如果我不知道html元素是img还是视频怎么办?

[英]What if I don't know whether an html element will be img or video?

I have a form with a file input. 我有一个带有文件输入的表格。 As each file is selected I add a preview of the image/video and a textarea for a caption. 选择每个文件后,我会添加图像/视频的预览和用于字幕的文本区域。

In order to display the preview, I need either: 为了显示预览,我需要:

  • a tag that can handle both image and video (I doubt this exists), or 可以同时处理图像和视频的标签(我怀疑这是存在的),或者
  • a client-side way to determine whether a file is an image or a video. 一种确定文件是图像还是视频的客户端方法。

Assuming you want to detect and then do something based on the element type, this jQuery should suffice. 假设您要检测然后根据元素类型执行某些操作,那么此jQuery就足够了。

var target = $('.media-element');

if ( target.is( "video" ) ) {
 do something
}

else if ( target.is('img')){
 do something else
}

else {return false;}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Html 选择器返回一个 html 集合,我不知道如何访问需要更改的元素 - Html selector returns an html collection and I don't know how to get to the element I need to make changes on jQuery无法在html中工作,我不知道自己在做什么错 - jQuery not working in html and I don't know what i'm doing wrong 我不知道如何在html中显示此内容。 这个脚本有什么问题? - I don't know how to make this show within the html. what is wrong with this script? 我不知道我做错了什么,但是我的带有 js 和 css 的 .html 不会显示必须显示的内容 - I don't know what I'm doing wrong but my .html with js and css won't show what is has to show 字幕轨道 HTML5 元素不要在视频中更新 - Subtitle Track HTML5 Element Don't Update in Video 关于代码中的函数,我不知道它们对什么意味着什么 - about functions in code that I don't know what they mean for what 如果我不知道其ID如何选择元素 - How to select element if I don't know its id jQuery正在剥离HTML,我不知道为什么 - jQuery is stripping out HTML, I don't know why HTML和JavaScript,JavaScript没有输出,我不知道为什么? - HTML and JavaScript, no output from javascript, i don't know why? 我不知道“渲染”有什么问题 - I don't know what's wrong with 'render'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM