[查看演示] 源码如下 ---------------------------------------------------------- <script>
var str = "经典论谈色眯眯的小疯狗啊噢哈哈呵呵啦啦当当"
var test = {"经典论谈":"经典论谈", "色眯眯":"色眯眯", "小疯狗":"小疯狗", "色眯眯的小疯狗":"色眯眯的小疯狗", "啊噢":"啊噢", "哈哈":"哈哈", "呵呵":"呵呵", "啦啦":"啦啦", "当当":"当当"}
aaa = new Date().getTime()
function getChineseWord(str, charPos, strLen)
{
var start = charPos - strLen < 0 ? 0 : charPos-strLen
var searchString = new Array()
for (var i=start; i<charPos; i++)
{
var end = i + strLen > str.length ? str.length : i + strLen
for (var j=end; j>=charPos; j--)
if (test[str.substring(i,j)]) searchString[searchString.length] = str.substring(i,j)
}
searchString.sort(function(a,b)
{
return b.length-a.length
})
//alert(new Date().getTime()-aaa)
return searchString.join(",")
}
//alert(getChineseWord(str, 1, 10))
document.onmousemove = function ()
{
var rng = document.body.createTextRange()
var charPos = 1
var strLen = 10
var wordRng = rng.duplicate()
wordRng.moveToPoint(window.event.x, window.event.y)
wordRng.expand("word")
if (window.event.x < wordRng.offsetLeft) wordRng.moveStart("word", -1)
var str = wordRng.text
for (var i=0; i<strLen && wordRng.text.match(/[\u4E00-\u9FA5]/); i++)
{
if (str.match(/^[\u4E00-\u9FA5]/) && wordRng.compareEndPoints("startToStart", rng) == 1)
{
wordRng.moveStart("word", -1)
if (wordRng.text.match(/[\u4E00-\u9FA5]/)) charPos++
}
if (str.match(/[\u4E00-\u9FA5]$/)) wordRng.moveEnd ("word", 1)
if (wordRng.text.replace(/[^\u4E00-\u9FA5]/g, "") == str) break
str = wordRng.text.replace(/[^\u4E00-\u9FA5]/g, "")
}
str = str.replace(/^\s|\s$/g, "")
window.status = getChineseWord(str ,charPos, strLen) +","+ str.charAt(charPos-1)
}
document.write(str.fontsize(2))
</script>
<body><div></body>
<div style="position: absolute; top: 10; right: 10; width: 148; height: 18;cursor:hand">
<input type="button" name="Button" value="查看源代码" onClick= 'window.location = "view-source:" + window.location.href'></div> |