为什么我不能使用jQuery定位图像元素 [英] Why can't I target the image element using jQuery
问题描述
我正在尝试添加jQuery以使其与我的网站一起使用,这是我第一次涉足jQuery.由于某种原因,我只是无法使它正常工作,而且我不确定为什么.调试输出中没有错误.这是我正在使用的确切代码:
I am trying to add jQuery to work with my web site and this is my first excursion into jQuery. For some reason, I just can't get it to work and I am unsure of why. There are no errors in the debug output. Here is the exact code I am using:
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.js"></script>
<script>
$(document).ready(function(){
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$('#Preview1').attr('src', e.target.result);
}
reader.readAsDataURL(input.files[0]);
}
}
$("#Image1").change(function(){
readURL(this);
alert("triggered");
});
});
</script>
</head>
<body>
<form>
<input type="file" id="#Image1">
<img src="#" id="Preview1">
</form>
</html>
它可以在jsfidle上运行,但是在chrome或firefox中不能使用.到目前为止,我所看到的所有答案都很简单,将其放在document.ready函数中,或者您丢失了分号或错误输入了URL.如果我有这个,我只是没有发现它.它也不适用于Google的最新版本的jQuery.
It works on jsfidle, but not when I try it in chrome or firefox. All of the answers I have seen so far are something simple like, put it inside a document.ready function or you are missing a semicolon or mistyped the URL. If I have this, I am just not spotting it. It also does not work with the latest version of jQuery from google.
推荐答案
从input
更改
<input type="file" id="#Image1">
收件人
<input type="file" id="Image1">
OR
使用\\
逃脱#
$("#\\#Image1").change(function () {
alert("triggered");
});
来自文档
使用任何元字符(例如!#$%&'()* +,./:;< =>?@ [] ^`{|}〜)作为文字部分名称,必须使用两个反斜杠将其转义:\\.
To use any of the meta-characters ( such as !"#$%&'()*+,./:;<=>?@[]^`{|}~ ) as a literal part of a name, it must be escaped with with two backslashes: \\.
这篇关于为什么我不能使用jQuery定位图像元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!