English 中文(简体)
关键词上的形象
原标题:Place image on top of keywords

我要说的是:

<div id="mainContent">some random text. bla bla bla.</div>

请允许我指出,这个例子中的序号是random。 我怎么能把形象放在这个词上?

换言之,我将不得不发现,与 Java文的四处,然后使用定期表述来找到关键词,然后可以总结一下,在一带内,关键词将一成不变。

我猜想,这方面最难部分的是,用一刀阔 the的词句??

edit

此外,我怎么能把形象放在这个词的中心? do 我不得不在一张桌子上总结?

最佳回答

第一,将四卷的内容储存在:

var x = document.getElementById( abc ).innerHTML;

然后,用包装版取代关键词。

var keyword = "test";
x.replace(keyword, "<span>"+keyword+"</span>");

之后又回去;

document.getElementById( abc ).innerHTML = x; 

我认为,最好利用这一点:

x.replace(  + keyword +  , "<span>"+keyword+"</span>");

符合字边界。

问题回答

For the answer you accepted, you have to know the id of the div that has the keyword. This solution is a bit more dynamic and uses jquery to find all divs with your keyword so you could place images on all of them.

JSFIDDLE

var keyword = "random";
var found = $( div:contains("  + keyword + ") );

$.each(found, function(){
    var wrapper =  <span class="whatever">  + keyword +  </span> ;
    var oldHtml = $(this).html();

    $(this).html(oldHtml.replace(keyword, wrapper));

});




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签