开发者

How do you draw a circle around something?

Using html, css, javascript and jQuery, I would like to present the user with a paragraph with the directions: "Read the story. Then, circle all the nouns."

Q: How would I dr开发者_运维百科aw a circle around a word when the user clicks on it? It's ok if every word has to be in its own span tag.


For the following HTML:

<p id="circleNouns">Now is the time for all good men to come to the aid of their country.</p>

First, let's wrap each word of the text in <span> tags with jQuery:

var $cn = $('#circleNouns'),
    cnText = $cn.text();
cnText = cnText.replace(/\s/g, '</span> <span>');
$cn.html('<span>' + cnText + '</span>');

Then if we style a class to draw a circle like border:

.selected { 
    border:1px solid red;
    border-radius:30px;
    -webkit-border-radius:30px;
    -moz-border-radius:30px;
}

We can simply add the following click event:

$cn.find('span').click(function(e) {
    $(this).toggleClass('selected');
});

Then however the page is submitted you can check $cn.find('.selected') each .html() against your list of nouns to see if the answers are correct.

Here's a working example →


You can use transparent PNGs of different sizes, small, medium, large to circle different sizes of words.

So when the user clicks the span/div , use javascript to overlay that PNG on the word, and resize it to best size dynamically.

But I would still suggest why not use a simple HTML based border to mark it ? It can be a border with rounded edges with minor css to make it more fancy

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜