先使用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中如何給圖片添加點擊事件的詳解的詳細內容,
一篇文章Stimulus:連接HTML和JavaScript的橋梁,實現簡單的controller,并學習了Stimulus是如何連接HTML與JavaScript的。現在我們使用Stimulus來實現復制文本到粘貼板的按鈕。
比如說,我們現在有一個需求,就是幫助用戶生成密碼,在密碼旁邊放置一個按鈕,點擊按鈕后密碼就被拷貝到粘貼板上了,這樣就方便用戶使用這個密碼了。
打開public/index.html,修改body內容,填充一個簡單的按鈕,如下:
<div>
PIN: <input type="text" value="1234" readonly>
<button>Copy to Clipboard</button>
</div>
下一步,創建src/controllers/clipboard_controller.js,然后添加一個copy()方法:
import { Controller } from "@hotwired/stimulus"
export default class extends Controller {
copy() {
}
}
然后,給div添加data-controller=“clipboard”。只要是給元素添加了data-controller屬性,Stimulus就會連接一個controller實例。
<div data-controller="clipboard">
我們還需要一個對輸入框的引用,這樣我們就可以在調用粘貼板API之前獲取輸入框的內容。給文本框添加data-clipboard-target=“source“:
PIN: <input data-clipboard-target="source" type="text" value="1234" readonly>
在controller中定義一個target,然后就可以通過this.sourceTarget訪問文本框了。
import { Controller } from "@hotwired/stimulus"
export default class extends Controller {
static targets = [ "source" ]
copy() {
}
}
解釋一下這個targets:
當Stimulus加載你的controller類時,它會查看靜態數組targets的字符串元素,對于每一個字符串,Stimulus會在controller中添加3個屬性。在這里,對于“source”,會添加如下屬性:
this.sourceTarget 在controller的域內的第一個source
this.sourceTargets 在controller的域內所有的source組成的一個數組
this.hasSourceTarget 在controller的域內是否有source
我們希望點擊按鈕時調用controller中的copy()方法,所以我們需要添加data-action=“clipboard#copy“
<button data-action="clipboard#copy">Copy to Clipboard</button>
你可以已經注意到在上面的動作描述符中省略了click->。那是因為Stimulus給button設置了click作為它默認的事件。
某些其他元素也有默認事件。下面是個全部列表:
元素 | 默認事件 |
a | click |
button | click |
details | toggle |
form | submit |
input | input |
input type=“submit” | click |
select | change |
textarea | input |
最終,在copy()方法中,我們獲取輸入框的內容,調用粘貼板API
copy() {
navigator.clipboard.writeText(this.sourceTarget.value)
}
刷新頁面,點擊按鈕,然后快捷鍵粘貼到Greet按鈕前到輸入框,可以看到1234。
到目前為止,在頁面上同一時間只有一個controller實例。在頁面上同時有一個controller的多個實例也是很正常的。
我們的controller是可以復用的,只要你需要在頁面上添加復制內容的按鈕,無論是哪個頁面,只要把對應的屬性值寫好,我們的controller都是生效的。
還是上面的例子,再添加另外一個復制按鈕:
<div data-controller="clipboard">
PIN: <input data-clipboard-target="source" type="text" value="3737" readonly>
<button data-action="clipboard#copy" class="clipboard-button">Copy to Clipboard</button>
</div>
刷新頁面,驗證一下兩個復制按鈕是否都生效。
我們再添加一個可以復制的元素,不用button,我們用a標簽,
<div data-controller="clipboard">
PIN: <input data-clipboard-target="source" type="text" value="6666" readonly>
<a href="#" data-action="clipboard#copy" class="clipboard-button">Copy to Clipboard</a>
</div>
Stimulus允許我們使用任何元素,只要它設置了合適的data-action屬性,就可以觸發復制。
這個例子里,要注意一點,點擊鏈接會使瀏覽器追蹤a標簽內的href屬性跳轉,可以取消這種默認行為,只需要在action中調用 event.preventDefault()就可以了。
copy(event) {
event.preventDefault()
navigator.clipboard.writeText(this.sourceTarget.value)
}
還有另外一個方法,拷貝粘貼板上
copy(event) {
event.preventDefault()
this.sourceTarget.select()
document.execCommand("copy")
}
在本文中,我們看了一個在現實中把瀏覽器API包裝在Stimulus的controller中的例子。還有一個controller的多個實例如何同時出現在頁面上,我們還探索了actions和targets如何保持HTML和JavaScript的松散耦合。
下一篇文章,我們將優化一下這個復制粘貼板的功能,讓它運行起來更加健壯。
Stimulus:瀏覽器不支持復制或者弱網條件下,怎么辦?
助CSS提供的animation與transform及filter濾鏡等屬性,我們可以使用CSS設置出精美的動畫效果,進一步可以使用CSS對HTML頁面基本元素、圖標等進行動畫設計,如按鈕效果,頁面加載圖標等。本文主要介紹加載圖標動畫設計,并進行實例分析。
CSS加載動畫設計
本例設計實現自動旋轉的加載圖標設計,在圖標旋轉過程中,動態改變顏色與狀態,實現效果如下所示:
動態加載loading設計效果
本例設計效果描述如上所示,一方面需要實現旋轉效果,另一方面需要實現顏色變化,所需掌握知識點主要包括CSS濾鏡filter,動畫屬性animation與@keyframes關鍵幀的定義等。各類所需基本語法說明如下:
1、flex布局
通過使用flex布局實現動畫層在web頁面中進行布局與定位,本例應用到justify-content屬性與
align-items屬性實現動畫層在頁面水平與垂直方向進行居中顯示。
2、before與after偽元素
偽元素是在html文件代碼中不存在的元素,但是其能夠在網頁瀏覽時表現出行為與效果,與真實存在的元素類似。其中before指在元素前加入的內容,after表示在元素后加入的內容。如我們在頁面中定義一個DIV層,則可使用before在前前添加新的內容,顯示不同效果。代碼如下:
before元素使用實例
在該實例中我們在dv表示的層之前添加了一個新的層效果,即.dv:before所定義的部分,頁面實際代碼中只有一個黃色層,生成的偽元素層為綠色層,實現效果如下:
偽元素層效果
3、線性漸變與透明度漸變
CSS提供元素原色的漸變效果,主要漸變類型包括徑向漸變與線性漸變。本例需要使用線性漸變實現背景效果。線性漸變(Linear Gradients)可以實現顏色漸變、透明的漸變等。如:
linear-gradient(0deg, blue, green 40%, red);
表示:從下到上,從藍色開始漸變、到高度40%位置是綠色漸變開始、最后以紅色結束;將其應用到我們偽元素定義案例中則可呈現如下效果:
偽元素層漸變效果
除使用以上方法實現漸變之外,還可以對透明度進行漸變設置,需要使用transparent參數表示透明。如:
linear-gradient(0deg,transparent,transparent 40%,red);則表示按照透明度進行漸變;
透明線性漸變效果如下圖所示:
現性漸變效果展示
4、圓形DIV與邊緣模糊設置
圓形DIV主要通過border-radius屬性設置,當其值設置為50%時即可形成圓形,邊緣模糊主要通過濾鏡filter實現。使用blur()設置高斯模糊。實例效果如下所示:
圓形div與高斯模糊
本例實現效果描述如上圖所示,其中外層div設置radius為50%,內層綠色部分設置高斯模糊。
5、動畫與旋轉基本知識
動畫效果主要通過animation屬性與@keyframes進行設置,前文已經進行了說明,本文不再詳細說明,如需閱讀,請自行查閱。
旋轉的加載動態效果基于以上基本語法進行設計與開發,實現主要代碼表述如下:
本例實現完整代碼
本頭條號長期關注編程資訊分享;編程課程、素材、代碼分享及編程培訓。如果您對以上方面有興趣或代碼錯誤、建議與意見,可在評論區回復。更多程序設計相關教程及實例分享,期待大家關注與閱讀!
*請認真填寫需求信息,我們會在24小時內與您取得聯系。