10/10/2018, 11:08
Lấy kí tự trên cửa sổ của website
chào các bạn . Mình có bị mắc vấn đề này ( cả về tư tưởng và code).
Làm thế nào để có thể dùng chuột lấy được một từ trên website và gửi từ đó về hệ thống của mình.
dưới đây là đoạn code mình tìm được trên w3school mình có thay đổi đi 1 chút nhưng chỉ lấy được cả đoạn văn bản đó ( sau khi chuột click vào)
Mình đang làm website tra cứu từ điển (~ tratu.vn) có chức năng tra cứu chéo.
Mong các anh em giúp đỡ
Làm thế nào để có thể dùng chuột lấy được một từ trên website và gửi từ đó về hệ thống của mình.
dưới đây là đoạn code mình tìm được trên w3school mình có thay đổi đi 1 chút nhưng chỉ lấy được cả đoạn văn bản đó ( sau khi chuột click vào)
Code:
<html>
<head>
<script type="text/javascript">
function whichElement(e)
{
var targ;
if (!e)
{
var e=window.event;
}
if (e.target)
{
targ=e.target;
}
else if (e.srcElement)
{
targ=e.srcElement.childNode[0].innerHTML;
}
if (targ.nodeType==3) // defeat Safari bug
{
targ = targ.parentNode;
}
var tname;
//tname=targ.tagName;
tname=targ.innerHTML;
alert("You clicked on a " + tname + " element.");
}
</script>
</head>
<body onmousedown="whichElement(event)">
<p>Click somewhere in the document. An alert box will alert the tag name of the element you clicked on.</p>
<h3>This is a header</h3>
<p>This is a paragraph</p>
</body>
</html>
Mong các anh em giúp đỡ
Bài liên quan





sau đó cắt lấy từ đầu tiên của tname
thực tế thì sẽ tra cả cụm từ khi đó sẽ dùng tname=tname.reaplace(' ','+')
sau đó gửi tới service để tìm kiếm ví dụ window.open("http:\\myweb.com?q="+tname);
bây giờ thì người ta dùng ajax hết chứ không open như trên
goodluck!!
tname=targ.innerHTML; var array= tname.split(' ',100); tname = array[4]; // giả sử ta lấy ra từ "mẫu" trong chuỗi(đây là ví dụ mẫu)Người dùng họ dùng chuột trái để quét từ mỗi 1 từ là "mẫu" trong cụm từ đó(tname lấy về được) thì làm thế nào để nhận diện được từ mà họ quét trong cả cụm mà tname nhận về.
Và bài trên mình sử dụng event: onmousedown còn sự kiện nào khác giống như sự kiện onselect trong textbox ( form) không bạn( để quét từ nào đó) ?
[=========> Bổ sung bài viết <=========]
Không có bác nào vào tham gia giúp em một lúc ạ
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head> <title>Untitled Page</title> <script type="text/javascript"> function autoSelect(selectTarget) { if(selectTarget != null && ((selectTarget.childNodes.length == 1//Count selectTarget sub object = 1 ://sl element = 1 de no co the la doan text && selectTarget.childNodes[0].nodeName == "#text"/*selectTarget co phan tu con la doan text = #text*/) || (selectTarget.tagName == "INPUT" && selectTarget.type == "text"/*Loai bo input dang button*/))) { if(window.getSelection) //Safari, chrome , FF { var sel = window.getSelection(); alert("1: " + sel); } else //IE 6-8 { var range = document.selection.createRange(); alert("2: " + range.text); } } //Finish if..... }//Finish function : autoSelect() </script> </head> <body> <h4 style="margin-bottom: 0;">A <code>div</code> Element:</h4> <div onclick="autoSelect(this);"> Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Nam ultrices vestibulum elit. Mauris congue sapien sed dolor. Pellentesque sem augue, porttitor id, placerat ac, congue ac, eros. Etiam fermentum consectetuer pede. Donec tincidunt. Suspendisse non nisi. In hac habitasse platea dictumst. In hac habitasse platea dictumst. Integer porta egestas sapien. </div> <h4 style="margin-bottom: 0;">An <code>input</code> Element:</h4> <input type="text" size="50" onclick="autoSelect(this);" value="Lorem ipsum dolor sit amet, consectetuer adipiscing elit."> <h4 style="margin-bottom: 0;">A <code>textarea</code> Element:</h4> <textarea rows="5" cols="30" onclick="autoSelect(this);"> Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Nam ultrices vestibulum elit. Mauris congue sapien sed dolor. Pellentesque sem augue, porttitor id, placerat ac, congue ac, eros. Etiam fermentum consectetuer pede. </textarea> <h4 style="margin-bottom: 0;">A <code>pre</code> Element:</h4> <pre onclick="autoSelect(this);"> function toggle_visibility(id) { var e = document.getElementById(id); if(e.style.display == 'none') e.style.display = 'block'; else e.style.display = 'none'; } </pre> </body> </html>Bài khá hay, nhưng mọi người tìm xem có cách nào để ta có thể lấy được một chữ bất kỳ mà không cần :
window.onload = function() {
document.getElementsByTagName("div").onClick = function() {
//Code của bạn nằm đây
}
$(document).ready(function() {
$("div").click(function() {
//Code của bạn ở đây
});
});
Thân.
[x]