、下面是三種button點擊的方法。
二、第2種方法就看我下面的表現了。
做個按鈕
圖1
圖2
加入javascript,dom取得按鈕元素。
圖3
添加click監聽函數。
圖5
圖6
先使用jQuery選擇器獲取到想要綁定click事件的img元素,然后可以直接綁定click方法,也可以通過bind方法綁定。這里詳細介紹一下bind方法。jQuery 事件 - bind() 方法 —— 定義和用法
html中如何給圖片添加點擊事件的詳解
bind() 方法為被選元素添加一個或多個事件處理程序,并規定事件發生時運行的函數。
jQuery 事件 - bind() 方法 ——將事件和函數綁定到元素
規定向被選元素添加的一個或多個事件處理程序,以及當事件發生時運行的函數。
jQuery 事件 - bind() 方法——語法
1 $(selector).bind(event,data,function)
jQuery 事件 - bind() 方法——參數描述
event 必需。規定添加到元素的一個或多個事件。由空格分隔多個事件。必須是有效的事件。
data 可選。規定傳遞到函數的額外數據。
function 必需。規定當事件發生時運行的函數。
實例:
1 //直接給所有img標簽綁定click事件
2 $("img").click(function(){
3 alert('你點擊了圖片');
4 })
5
6 //使用bind方法綁定click事件
7 $("img").bind("click",function(){
8 alert('你點擊了圖片');
9 })
Html 的img標簽添加點擊事件
1 package com.topnews;
2
3 import java.util.ArrayList;
4
5 import android.annotation.SuppressLint;
6 import android.app.Activity;
7 import android.app.Fragment;
8 import android.content.Context;
9 import android.content.Intent;
10 import android.graphics.Bitmap;
11 import android.os.AsyncTask;
12 import android.os.Bundle;
13 import android.text.TextUtils;
14 import android.util.Log;
15 import android.view.View;
16 import android.view.ViewGroup.LayoutParams;
17 import android.webkit.WebChromeClient;
18 import android.webkit.WebSettings;
19 import android.webkit.WebView;
20 import android.webkit.WebViewClient;
21 import android.webkit.WebSettings.LayoutAlgorithm;
22 import android.widget.Button;
23 import android.widget.FrameLayout;
24 import android.widget.ProgressBar;
25 import android.widget.TextView;
26
27 import com.topnews.base.BaseActivity;
28 import com.topnews.bean.NewsEntity;
29 import com.topnews.service.NewsDetailsService;
30 import com.topnews.tool.BaseTools;
31 import com.topnews.tool.DataTools;
32 import com.topnews.tool.DateTools;
33
34 @SuppressLint("JavascriptInterface")
35 public class DetailsActivity extends BaseActivity {
36 private TextView title;
37 private ProgressBar progressBar;
38 private FrameLayout customview_layout;
39 private String news_url;
40 private String news_title;
41 private String news_source;
42 private String news_date;
43 private NewsEntity news;
44 private TextView action_comment_count;
45 WebView webView;
46
47 @Override
48 protected void onCreate(Bundle savedInstanceState) {
49 // TODO Auto-generated method stub
50 super.onCreate(savedInstanceState);
51 setContentView(R.layout.details);
52 setNeedBackGesture(true);// 設置需要手勢監聽
53 getData();
54 initView();
55 initWebView();
56 }
57
58 /* 獲取傳遞過來的數據 */
59 private void getData() {
60 news = (NewsEntity) getIntent().getSerializableExtra("news");
61 news_url = news.getSource_url();
62 news_title = news.getTitle();
63 news_source = news.getSource();
64 news_date = 65DateTools.getNewsDetailsDate(String.valueOf(news.getPublishTime()));
66 }
67
68 private void initWebView() {
69 webView = (WebView) findViewById(R.id.wb_details);
70 LayoutParams params = new 71LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
72 if (!TextUtils.isEmpty(news_url)) {
73 WebSettings settings = webView.getSettings();
74 settings.setJavaScriptEnabled(true);// 設置可以運行JS腳本
75 // settings.setTextZoom(120);//Sets the text zoom of the page in
76 // percent. The default is 100.
77 settings.setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
78 // settings.setUseWideViewPort(true); //打開頁面時, 自適應屏幕
79 // settings.setLoadWithOverviewMode(true);//打開頁面時, 自適應屏幕
80 settings.setSupportZoom(false);// 用于設置webview放大
81 settings.setBuiltInZoomControls(false);
82 webView.setBackgroundResource(R.color.transparent);
83 // 添加js交互接口類,并起別名 imagelistner
84 webView.addJavascriptInterface(new 85JavascriptInterface(getApplicationContext()), "imagelistner");
86 webView.setWebChromeClient(new MyWebChromeClient());
87 webView.setWebViewClient(new MyWebViewClient());
88 Log.i("info", "news_url:" + news_url);
89 Log.i("info", "news_title:" + news_title);
90 Log.i("info", "news_source:" + news_source);
91 Log.i("info", "news_date:" + news_date);
92 new MyAsnycTask().execute(news_url, news_title, news_source + " " + 93news_date);
94 }
95 }
96
97 private void initView() {
98 title = (TextView) findViewById(R.id.title);
99 progressBar = (ProgressBar) findViewById(R.id.ss_htmlprogessbar);
100 customview_layout = (FrameLayout) 101findViewById(R.id.customview_layout);
102 // 底部欄目
103 action_comment_count = (TextView) 104findViewById(R.id.action_comment_count);
105
106 progressBar.setVisibility(View.VISIBLE);
107 title.setTextSize(13);
108 title.setVisibility(View.VISIBLE);
109 title.setText(news_url);
110 action_comment_count.setText(String.valueOf(news.getCommentNum()));
111 }
112
113 @Override
114 public void onBackPressed() {
115 super.onBackPressed();
116 overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);
117 }
118
119 private class MyAsnycTask extends AsyncTask<string, string,="" string=""> {
120
121 @Override
122 protected String doInBackground(String... urls) {
123 String data = NewsDetailsService.getNewsDetails(urls[0], urls[1], urls[2]);
124 Log.i("info", "MyAsnycTask.data:" + data);
125 return data;
126 }
127
128 @Override
129 protected void onPostExecute(String data) {
130 webView.loadDataWithBaseURL(null, data, "text/html", "utf-8", null);
131 }
132 }
133
134 // 注入js函數監聽
135 private void addImageClickListner() {
136 // 這段js函數的功能就是,遍歷所有的img幾點,并添加onclick函數,在還是執137 行的時候調用本地接口傳遞url過去
138 webView.loadUrl("javascript:(function(){" + "var objs = 139document.getElementsByTagName(\"img\");" + "var imgurl=''; "
140 + "for(var i=0;i<objs.length;i++) "="" +="" "{"="" 141"imgurl+="objs[i].src+',';"" objs[i].onclick="function()" {="" 142window.imagelistner.openimage(imgurl);="" }="" "}"="" "})()");="" js通信接口="" 143public="" class="" javascriptinterface="" private="" context="" context;="" 144javascriptinterface(context="" context)="" this.context="context;" void="" 145openimage(string="" img)="" string[]="" imgs="img.split(",");" 146arraylist<string=""> imgsUrl = new ArrayList<string>();
147 for (String s : imgs) {
148 imgsUrl.add(s);
149 Log.i("圖片的URL>>>>>>>>>>>>>>>>>>>>>>>", s);
150 }
151 Intent intent = new Intent();
152 intent.putStringArrayListExtra("infos", imgsUrl);
153 intent.setClass(context, ImageShowActivity.class);
154 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
155 context.startActivity(intent);
156 }
157 }
158
159 // 監聽
160 private class MyWebViewClient extends WebViewClient {
161 @Override
162 public boolean shouldOverrideUrlLoading(WebView view, String url) {
163 return super.shouldOverrideUrlLoading(view, url);
164 }
165
166 @Override
167 public void onPageFinished(WebView view, String url) {
168 view.getSettings().setJavaScriptEnabled(true);
169 super.onPageFinished(view, url);
170 // html加載完成之后,添加監聽圖片的點擊js函數
171 addImageClickListner();
172 progressBar.setVisibility(View.GONE);
173 webView.setVisibility(View.VISIBLE);
174 }
175
176 @Override
177 public void onPageStarted(WebView view, String url, Bitmap favicon) {
178 view.getSettings().setJavaScriptEnabled(true);
179 super.onPageStarted(view, url, favicon);
180 }
181
182 @Override
183 public void onReceivedError(WebView view, int errorCode, String description, 184String failingUrl)
185 {
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);
}
}
}</string></objs.length;i++)></string,>
// NewsDetailsService.java
1 package com.topnews.service;
2
3 import java.io.IOException;
4 import org.jsoup.Jsoup;
5 import org.jsoup.nodes.Document;
6 import org.jsoup.nodes.Element;
7
8 import android.text.TextUtils;
9
10 public class NewsDetailsService {
11 public static String getNewsDetails(String url, String news_title,
12 String news_date) {
13 Document document = null;
14 String data = "" +
15 "<center><h2 style="'font-size:16px;'">" + news_title + "</h2></center>";
16 data = data + "<p align="'left'" style="'margin-left:10px'">"
17 + "<span style="'font-size:10px;'">"
18 + news_date
19 + "</span>"
20 + "</p>";
21 data = data + "<hr size="'1'">";
22 try {
23 document = Jsoup.connect(url).timeout(9000).get();
24 Element element = null;
25 if (TextUtils.isEmpty(url)) {
26 data = "";
27 element = document.getElementById("memberArea");
28 } else {
29 element = document.getElementById("artibody");
30 }
31 if (element != null) {
32 data = data + element.toString();
33 }
34 data = data + "";
35 } catch (IOException e) {
36 e.printStackTrace();
37 }
38 return data;
39 }
40 }
以上就是html中如何給圖片添加點擊事件的詳解的詳細內容,
天在寫一個關于 html 中 select 下拉元素選擇的動態事件時,發現如果使用 javascript 中的 click 事件的話,會被執行兩次。網上查了一下資料,發現 select 元素下拉選中事件并不適合使用 click 來觸發,而要使用 change 事件。
html代碼
<select name="mochu" id="mochu"> <option value="1">下拉1</option> <option value="2">下拉2</option> <option value="3">下拉3</option> <option value="4">下拉4</option> </select> <script> $('#mochu').click(function(){ console.log($(this).val()); }); </script>
點擊下拉后,輸出信息如下:
通過上圖可以發現,如果 select 元素的下拉菜單使用 click 事件的話,會輸出兩次結果,也就說在展開下拉菜單之前與選擇下拉菜單之后分別觸發了 click 事件,所以 select 下拉選項的觸發事件,不能使用 click,而使用 change 事件。
示例代碼
<select name="mochu" onchange="GetVal(this)"> <option value="下拉1">選項1</option> <option value="下拉2">選項2</option> <option value="下拉3">選項3</option> <option value="下拉4">選項4</option> </select> <script> function GetVal(obj){ //代表的是選中項的index索引 var index = obj.selectedIndex; //代表的是選中項的的值 var val = obj.options[index].value; //代表的是選中項的text var txt = obj.options[index].text; console.log(index); console.log(val); console.log(txt); } </script>
輸出結果:
*請認真填寫需求信息,我們會在24小時內與您取得聯系。