First use the jQuery selector to get the img element that you want to bind the click event to, and then you can bind the click method directly or bind it through the bind method. Here is a detailed introduction to the bind method.
jQuery event - bind() method - Definition and usage
The bind() method adds one or more event handlers to the selected element and specifies the event Function to run when this occurs.
jQuery Events - bind() method - Bind events and functions to elements
Specifies one or more event handlers to add to the selected element and run when the event occurs The function.
jQuery event - bind() method - syntax
$(selector).bind(event,data,function)
jQuery event - bind() method - parameter description
Event Required. Specifies one or more events to be added to the element. Multiple events separated by spaces. Must be a valid event.
Data is optional. Specifies additional data to be passed to the function.
Function Required. Specifies a function to run when an event occurs.
Example:
//直接给所有img标签绑定click事件 $("img").click(function(){ alert('你点击了图片'); }) //使用bind方法绑定click事件 $("img").bind("click",function(){ alert('你点击了图片'); })
Html's img tag adds a click event
package com.topnews; import java.util.ArrayList; import android.annotation.SuppressLint; import android.app.Activity; import android.app.Fragment; import android.content.Context; import android.content.Intent; import android.graphics.Bitmap; import android.os.AsyncTask; import android.os.Bundle; import android.text.TextUtils; import android.util.Log; import android.view.View; import android.view.ViewGroup.LayoutParams; import android.webkit.WebChromeClient; import android.webkit.WebSettings; import android.webkit.WebView; import android.webkit.WebViewClient; import android.webkit.WebSettings.LayoutAlgorithm; import android.widget.Button; import android.widget.FrameLayout; import android.widget.ProgressBar; import android.widget.TextView; import com.topnews.base.BaseActivity; import com.topnews.bean.NewsEntity; import com.topnews.service.NewsDetailsService; import com.topnews.tool.BaseTools; import com.topnews.tool.DataTools; import com.topnews.tool.DateTools; @SuppressLint("JavascriptInterface") public class DetailsActivity extends BaseActivity { private TextView title; private ProgressBar progressBar; private FrameLayout customview_layout; private String news_url; private String news_title; private String news_source; private String news_date; private NewsEntity news; private TextView action_comment_count; WebView webView; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.details); setNeedBackGesture(true);// 设置需要手势监听 getData(); initView(); initWebView(); } /* 获取传递过来的数据 */ private void getData() { news = (NewsEntity) getIntent().getSerializableExtra("news"); news_url = news.getSource_url(); news_title = news.getTitle(); news_source = news.getSource(); news_date = DateTools.getNewsDetailsDate(String.valueOf(news.getPublishTime())); } private void initWebView() { webView = (WebView) findViewById(R.id.wb_details); LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); if (!TextUtils.isEmpty(news_url)) { WebSettings settings = webView.getSettings(); settings.setJavaScriptEnabled(true);// 设置可以运行JS脚本 // settings.setTextZoom(120);//Sets the text zoom of the page in // percent. The default is 100. settings.setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN); // settings.setUseWideViewPort(true); //打开页面时, 自适应屏幕 // settings.setLoadWithOverviewMode(true);//打开页面时, 自适应屏幕 settings.setSupportZoom(false);// 用于设置webview放大 settings.setBuiltInZoomControls(false); webView.setBackgroundResource(R.color.transparent); // 添加js交互接口类,并起别名 imagelistner webView.addJavascriptInterface(new JavascriptInterface(getApplicationContext()), "imagelistner"); webView.setWebChromeClient(new MyWebChromeClient()); webView.setWebViewClient(new MyWebViewClient()); Log.i("info", "news_url:" + news_url); Log.i("info", "news_title:" + news_title); Log.i("info", "news_source:" + news_source); Log.i("info", "news_date:" + news_date); new MyAsnycTask().execute(news_url, news_title, news_source + " " + news_date); } } private void initView() { title = (TextView) findViewById(R.id.title); progressBar = (ProgressBar) findViewById(R.id.ss_htmlprogessbar); customview_layout = (FrameLayout) findViewById(R.id.customview_layout); // 底部栏目 action_comment_count = (TextView) findViewById(R.id.action_comment_count); progressBar.setVisibility(View.VISIBLE); title.setTextSize(13); title.setVisibility(View.VISIBLE); title.setText(news_url); action_comment_count.setText(String.valueOf(news.getCommentNum())); } @Override public void onBackPressed() { super.onBackPressed(); overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right); } private class MyAsnycTask extends AsyncTask<String, String, String> { @Override protected String doInBackground(String... urls) { String data = NewsDetailsService.getNewsDetails(urls[0], urls[1], urls[2]); Log.i("info", "MyAsnycTask.data:" + data); return data; } @Override protected void onPostExecute(String data) { webView.loadDataWithBaseURL(null, data, "text/html", "utf-8", null); } } // 注入js函数监听 private void addImageClickListner() { // 这段js函数的功能就是,遍历所有的img几点,并添加onclick函数,在还是执行的时候调用本地接口传递url过去 webView.loadUrl("javascript:(function(){" + "var objs = document.getElementsByTagName(\"img\");" + "var imgurl=''; " + "for(var i=0;i<objs.length;i++) " + "{" + "imgurl+=objs[i].src+',';" + " objs[i].onclick=function() " + " { " + " window.imagelistner.openImage(imgurl); " + " } " + "}" + "})()"); } // js通信接口 public class JavascriptInterface { private Context context; public JavascriptInterface(Context context) { this.context = context; } public void openImage(String img) { // String[] imgs = img.split(","); ArrayList<String> imgsUrl = new ArrayList<String>(); for (String s : imgs) { imgsUrl.add(s); Log.i("图片的URL>>>>>>>>>>>>>>>>>>>>>>>", s); } Intent intent = new Intent(); intent.putStringArrayListExtra("infos", imgsUrl); intent.setClass(context, ImageShowActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(intent); } } // 监听 private class MyWebViewClient extends WebViewClient { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { return super.shouldOverrideUrlLoading(view, url); } @Override public void onPageFinished(WebView view, String url) { view.getSettings().setJavaScriptEnabled(true); super.onPageFinished(view, url); // html加载完成之后,添加监听图片的点击js函数 addImageClickListner(); progressBar.setVisibility(View.GONE); webView.setVisibility(View.VISIBLE); } @Override public void onPageStarted(WebView view, String url, Bitmap favicon) { view.getSettings().setJavaScriptEnabled(true); super.onPageStarted(view, url, favicon); } @Override public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { progressBar.setVisibility(View.GONE); super.onReceivedError(view, errorCode, description, failingUrl); } } private class MyWebChromeClient extends WebChromeClient { @Override public void onProgressChanged(WebView view, int newProgress) { // TODO Auto-generated method stub if (newProgress != 100) { progressBar.setProgress(newProgress); } super.onProgressChanged(view, newProgress); } } }
// NewsDetailsService.java
package com.topnews.service; import java.io.IOException; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import android.text.TextUtils; public class NewsDetailsService { public static String getNewsDetails(String url, String news_title, String news_date) { Document document = null; String data = "<body>" + "<center><h2 id="nbsp-nbsp-news-title-nbsp-nbsp">" + news_title + "</h2></center>"; data = data + "<p align='left' style='margin-left:10px'>" + "<span style='font-size:10px;'>" + news_date + "</span>" + "</p>"; data = data + "<hr size='1' />"; try { document = Jsoup.connect(url).timeout(9000).get(); Element element = null; if (TextUtils.isEmpty(url)) { data = ""; element = document.getElementById("memberArea"); } else { element = document.getElementById("artibody"); } if (element != null) { data = data + element.toString(); } data = data + "</body>"; } catch (IOException e) { e.printStackTrace(); } return data; } }
The above is the detailed content of Detailed explanation of how to add click events to images in html. For more information, please follow other related articles on the PHP Chinese website!

本篇文章带大家了解一下HTML(超文本标记语言),介绍一下HTML的本质,HTML文档的结构、HTML文档的基本标签和图像标签、列表、表格标签、媒体元素、表单,希望对大家有所帮助!

不算。html是一种用来告知浏览器如何组织页面的标记语言,而CSS是一种用来表现HTML或XML等文件样式的样式设计语言;html和css不具备很强的逻辑性和流程控制功能,缺乏灵活性,且html和css不能按照人类的设计对一件工作进行重复的循环,直至得到让人类满意的答案。

总结了一些web前端面试(笔试)题分享给大家,本篇文章就先给大家分享HTML部分的笔试题(附答案),大家可以自己做做,看看能答对几个!

HTML5中画布标签是“<canvas>”。canvas标签用于图形的绘制,它只是一个矩形的图形容器,绘制图形必须通过脚本(通常是JavaScript)来完成;开发者可利用多种js方法来在canvas中绘制路径、盒、圆、字符以及添加图像等。

在html中,document是文档对象的意思,代表浏览器窗口的文档;document对象是window对象的子对象,所以可通过“window.document”属性对其进行访问,每个载入浏览器的HTML文档都会成为Document对象。

html5废弃了dir列表标签。dir标签被用来定义目录列表,一般和li标签配合使用,在dir标签对中通过li标签来设置列表项,语法“<dir><li>列表项值</li>...</dir>”。HTML5已经不支持dir,可使用ul标签取代。

3种取消方法:1、给td元素添加“border:none”无边框样式即可,语法“td{border:none}”。2、给td元素添加“border:0”样式,语法“td{border:0;}”,将td边框的宽度设置为0即可。3、给td元素添加“border:transparent”样式,语法“td{border:transparent;}”,将td边框的颜色设置为透明即可。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.
