English 中文(简体)
添加无标记或名称的图象/泡沫
原标题:Adding a tool tip / bubble display without span tag or title

当用户 h住某些文本/关键词时,我如何展示工具? 这份案文从数据库中直接检索,因此我无法将这些关键词上的任何间隔或批号或所有权信息添加。 是否有办法自动为该页所含某些词制定工具?

让我知道,在如何解决这一问题方面是否有任何很好的指导。

最佳回答
// Create tooltips dictionary
$tooltips = Array("Word1"=>"This word is word number 1", 
                  "Word2"=>"This word is word number 2");

$content = "Here are Word1 and Word2";
foreach ($tooltips as $word=>$tip){
    $content = preg_replace("/(".$word.")/", "<span title= ".$tip." >$1</span>", $content);
}
echo $content;
问题回答

此前我不得不这样做。 实际上,我在这里回答了一个类似的问题:。 (在我旁搜寻时)。

在此,我用来做动态工具:

// keyword : tooltip
keywords = {
     hello world  :  a common first example program ,
     goodbye cruel world  :  the opposite of hello world 
};
function insertTooltips (domNode) {
    if (domNode.nodeType === Node.ELEMENT_NODE) { // We only want to scan html elements
        var children = domNode.childNodes;
        for (var i=0;i<children.length;i++) {
            var child = children[i];

            // Filter out unwanted nodes to speed up processing.
            // For example, you can ignore  SCRIPT  nodes etc.
            if (
                child.nodeName  !=  span  ||
                child.className !=  tooltip_span 
            ) {
                insertTooltips(child); // Recurse!
            }
        }
    }
    else if (domNode.nodeType === Node.TEXT_NODE) { // Process text nodes
        var text = domNode.nodeValue;

        // This is another place where it might be prudent to add filters

        for (var i in keywords) {
          if (keywords.hasOwnProperty(i)) {
            var match = text.indexOf(i); // you may use search instead
            if (match != -1) {
                // This is how you wrap the keyword in a span:

                // create a span:
                var span = document.createElement( SPAN );

                // split text into 3 parts: before, mid and after
                var mid = domNode.splitText(match);
                mid.splitText(i.length);

                // then assign mid part to span
                mid.parentNode.insertBefore(span,mid);
                mid.parentNode.removeChild(mid);
                span.appendChild(mid);

                // now we can assign a mouseover event handler to the span
                span.onmouseover = function(){
                    showToolTip(keywords[i]);
                }

                // give the span a class name to prevent accidental
                // recursion into it:
                span.className =  tooltip_span ;
            }
          }
        }
    }
}

淋浴功能的实施留给读者。

基本的想法是利用OMM的操纵,以动态方式搜索和总结跨度的关键词,然后把 mo(或烟lick,直至你)的手推到这个时代,以显示工具。 在我的网站上,关键词/tooltip hash/object来自从数据库中提取的数据。

这样做比试图利用reg子进行更强,因为它防止了用阶级名称、背心和文字标签等关键词的意外处理。





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签