简体   繁体   English

如何编码HTML文本框内的图像的单击事件

[英]how to code click event for an image inside html text box

i have a html text box with an background image as below in asp.net mvc page. 我在asp.net mvc页面中有一个带有背景图像的html文本框。 an now i want to write click event for that back ground image alone. 现在,我只想为该背景图像编写点击事件。

      <input type="text" id="txtProjectCode"  readonly="readonly" style="background-image:url('/Content/Images/remove.png');
background-repeat:no-repeat;
background-position:right;
" /> 

how can i write click event handler for the background image of the html text box in javascript? 如何在javascript中为html文本框的背景图像编写click事件处理程序? if not in javascript any work around to do it? 如果不在javascript中解决该问题?

Wrap the input element within a div, and create an image element, set CSS position: relative; 将输入元素包装在div中,并创建一个图像元素,并设置CSS position: relative; on the parent div, and set position: absolute; 在父div上,并设置position: absolute; on the image, and by specifying the top/left properties of the image, you can position the image to overlay the input. 在图像上,并通过指定图像的top/left属性,可以top/left图像以覆盖输入。 If you want to overlay the input background, you can make the overlay image transparent and assign click event on the transparent image and it should work. 如果要覆盖输入背景,则可以使覆盖图像透明,并在透明图像上分配click事件,它应该可以工作。 Something like: 就像是:

<div style="position: relative;">

<input type="text" id="txtProjectCode" readonly="readonly" style="background-image:url('/Content/Images/remove.png'); background-repeat:no-repeat; background-position:right; " />
<img src="img.png" style="position: absolute; right: 0;" />

</div>

here is how to implement the overlay with js: jsfiddle 这是如何用js实现覆盖: jsfiddle

css: CSS:

#overlay{
    position: absolute;
    background-color: rgb(255,0,0);
    opacity:0.2;
    z-index:5;
}
#field{
    /*background-image:url('');*/
}

html: 的HTML:

<input id="field" type="text" /> 
<div id="overlay"></div>

js: js:

console.log(parseInt($('#field').css('border-width')));
var border = parseInt($('#field').css('border'));
$('#overlay').css({'width':($('#field').width()+2*border)+'px','height':($('#field').height()+3*border)+'px','left':($('#field').offset().left)+'px','top':($('#field').offset().top)+'px'});

$('#overlay').click(function(){
    console.log('clicked');
    $('#field').focus();
}).mousedown(function(){
    $('#field').mousedown();
}).mouseup(function(){
    $('#field').mouseup();
});

if you want to add text selection => add this $('#field').select(); 如果要添加文本选择=>添加此$('#field').select(); inside mousedown trigger or mouseup 在mousedown触发器或mouseup内部

https://stackoverflow.com/que42/css-background-image-onclickstions/96682 https://stackoverflow.com/que42/css-background-image-onclickstions/96682

It'st not possible this way, because background isn't part of the DOM. 这样是不可能的,因为背景不是DOM的一部分。 You can only acheve this by catching click event of the textarea and then examinating the coordinates of click. 您只能通过捕获文本区域的click event ,然后检查click的坐标来实现此目的。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM