动态调整div中的文本大小

时间:2023-01-27 17:29:05

This is my jsFiddle code . I am dynamically changing the text font to fix in the div. It is working fine. My text is in span, but I do not want to use span. If I remove the span then it stops working. I even changed my function from.

这是我的jsFiddle代码。我正在动态地修改文本字体以修改div。它运行得很好。我的文本在span中,但我不想使用span。如果我移除跨度,它就停止工作。我甚至改变了函数。

var ourText = $('span:visible:first', this);

to

 var ourText = $(targetid, this);

i want to use like this

我想用这个。

 <h1  id="h1" class='jtextfill'>
    Sample 
</h1>
<h2 id="h2"  class='jtextfill2' >
   aaSamp sfd sdfsf sfsddffds ss ssf fsdf sdf sfs fsdfs sdfsdf sdf sdfsfsfsdfsdfsf sdf sfsf sfs sf sf sd  sfsf sfsdsfd sfdfdf a ada  adf ad adasda a adsa s sfzz
</h2>

But it did not work.

但它没有起作用。

1 个解决方案

#1


0  

Your jquery selectors are malformed: write $( "#"+targetid ) instead of $(targetid). The latter construct references tags corresponding to the cntent of the parameter targetid - to reference ids, you have to prefix the id with the literal #. Note that this only applies to jquery and css selectors, in particular not to the argument of getElementById. See this fiddle (Everything said applies to ourText = $('#'+targetid, this); too, of course).

jquery选择器是畸形的:写入$(“#”+targetid)而不是$(targetid)。后者构造与参数targetid的cntent对应的引用标记—要引用id,必须用文字#作为id的前缀。注意,这只适用于jquery和css选择器,特别是不适用于getElementById的参数。看这个小提琴(所有的东西都适用于我们的文本= $('#'+targetid,这个);当然,)。

Update: This fiddle contains streamlined code.

更新:这个小提琴包含流线型代码。

I do not see how you could dispense of the 'span' element: to get height and width of the text you need an element to read the height/width attribute from. But you also need the container that defines the viewport into which you want to fit your text.

我不认为您可以省去“span”元素:要获得文本的高度和宽度,您需要一个元素来读取height/width属性。但是,您还需要一个容器来定义要适合您的文本的viewport。

#1


0  

Your jquery selectors are malformed: write $( "#"+targetid ) instead of $(targetid). The latter construct references tags corresponding to the cntent of the parameter targetid - to reference ids, you have to prefix the id with the literal #. Note that this only applies to jquery and css selectors, in particular not to the argument of getElementById. See this fiddle (Everything said applies to ourText = $('#'+targetid, this); too, of course).

jquery选择器是畸形的:写入$(“#”+targetid)而不是$(targetid)。后者构造与参数targetid的cntent对应的引用标记—要引用id,必须用文字#作为id的前缀。注意,这只适用于jquery和css选择器,特别是不适用于getElementById的参数。看这个小提琴(所有的东西都适用于我们的文本= $('#'+targetid,这个);当然,)。

Update: This fiddle contains streamlined code.

更新:这个小提琴包含流线型代码。

I do not see how you could dispense of the 'span' element: to get height and width of the text you need an element to read the height/width attribute from. But you also need the container that defines the viewport into which you want to fit your text.

我不认为您可以省去“span”元素:要获得文本的高度和宽度,您需要一个元素来读取height/width属性。但是,您还需要一个容器来定义要适合您的文本的viewport。